zoukankan      html  css  js  c++  java
  • [Xamarin] 透過 intent-filter 來接管 http ,製作偽瀏覽器 (转帖)

    使用Android 的朋友一定對這畫面不陌生
    Screenshot_2013-07-10-20-20-23
    在開啟網址的時候,或是Youtube連結的時候,因為Android 發現,你手機安裝的App有哪些可以支援這些東西的瀏覽

    所以,就可以使用甚麼東西來進行開啟,上面那個一個'偽瀏覽器'就是透過這方法做到的,當然這篇目的不是要做瀏覽器

    所以只會在開啟後取得該連結網址,首先我們來看看AndroidManifest.xml (加入AndroidManifest.xml可以參考這篇)


    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="com.donma.test">
        <uses-sdk android:targetSdkVersion="8" />
        <application>
            <activity android:name=".Activity123" android:label="偽覽器">
                <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <data android:scheme="http" />
                </intent-filter>
            </activity>
     
        </application>
    </manifest>

    2013-07-11_180839

    請注意,上面的Package name 一定要設,並且至少要有一個 . (dot) 以上,之前案例都沒有設定都可以順利執行但是這在裡面一定要設定

    不然會發生他會找不到對應的Activity 產生 Android java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

    這地方花debug花了一小點時間

    在這AndroidManifest.xml 中我指定了我有意圖會接官http 並且指定為.Activity123 來開啟

    關於 AndroidManifest.xml  這博大精深的設定可以參考Android 官方網站

    我們來看看 Activity123 Code 的部分

    using Android.App;
    using Android.Widget;
    using Android.OS;
     
    namespace CallActivity
    {
        [Activity(Name = "com.donma.test.Activity123", Label = "CallActivity1", MainLauncher = true, Icon = "@drawable/icon")]
        public class Activity123 : Activity
        {
           
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
                SetContentView(Resource.Layout.Main);
     
                var textView1 = FindViewById<TextView>(Resource.Id.textView1);
     
                //透過this.Intent 取得由系統發過來的Intent的文字資料
                textView1.Text = Intent.DataString;
     
            }
        }
    }
     

    其中,紅色文字的那一段Name一定要設定,讓系統可以抓到他,再來就是接到指令後可以透過this.Intent 來去抓到Intent中的資料(等於在java 開發中

    getIntent() )

    結果:
    Screenshot_2013-07-10-20-20-23
    開啟後抓取到觸發的網址

    Screenshot_2013-07-10-20-20-29
    這樣就可以做到很多事情,程式不難,設定比較許多枚枚角角.

    參考文章: http://www.vogella.com/articles/AndroidIntent/

  • 相关阅读:
    Ubuntu虚拟机磁盘空间不足的解决
    eclipse启动报错 JVM terminated. Exit code=1
    Ubuntu16.04 安装eclipse
    HDU 1710 Binary Tree Traversals(二叉树)
    Ubuntu16.04 搭建伪分布式Hadoop环境
    HDU 1560 DNA sequence(IDA*)
    Go的函数
    Go的包
    Go语言开发环境搭建
    go的循环
  • 原文地址:https://www.cnblogs.com/whatthehell/p/3444736.html
Copyright © 2011-2022 走看看