android:android Intent and IntentFilter,intentfilter

來源:互聯網
上載者:User

android:android Intent and IntentFilter,intentfilter

文章來自:http://blog.csdn.net/intbird

1,intentfilter說明:

如果說是將一個activity有其他應用開啟,或者是在webView 或者 bybird app中調用我們自己的activity,
那麼就可以使用intentfilter來過濾我們自己需要的activity;

*uri參數說明 Java Code:

final Uri uri =  Uri.parse("intbird://com.intbird.soft/testing/tested?name='Test'");        TextView textView = (TextView)findViewById(R.id.textView);        textView.setText(uri.getScheme() + "\n" //intbird                        + uri.getPort() + "\n"  //-1                        + uri.getAuthority() + "\n" //com.intbird.soft                        + uri.getHost() + "\n"  //com.intbird.soft                        + uri.getPath() + "\n" // /testing/tested                        + uri.getPathSegments() + "\n" // [tesing,tested]                        + uri.getQuery()+"\n"); // name='Text'
public void onClick(View v) {                Intent intent = new Intent();                intent.setAction("android.intent.action.VIEW");                intent.setData(uri);                PackageManager packageManager = getPackageManager();                List<ResolveInfo> activities = packageManager.queryIntentActivities(intent,0);                boolean isIntentSafe  = activities.size() > 0;                if(isIntentSafe){                    startActivity(intent);                }else{                    Toast.makeText(MainActivity.this, "匹配None", Toast.LENGTH_SHORT).show();                }            }

XML Code:

NOTE : 必須有category標籤
<activity            android:name=".MyIntentFilter"            android:label="@string/app_name" >            <intent-filter>                <category android:name="android.intent.category.DEFAULT"></category>                <action android:name="android.intent.action.VIEW"/>                <data android:scheme="intbird" android:host="com.intbird.soft"/>            </intent-filter>        </activity>
2,結合網頁調用加深理解

uri:
final Uri urihttp = Uri.parse(“https://www.baidu.com/s?wd=intbird“);
//schma : http
//host : www.baidu.com
//path : /s
//query : wd= intbird

XML

<activity            android:name=".MyIntentFilter2"            android:label="@string/app_name" >            <intent-filter>                <category android:name="android.intent.category.DEFAULT"/>                <action android:name="android.intent.action.VIEW"/>                <data android:scheme="https"  android:host="www.baidu.com" />            </intent-filter>        </activity>

匹配scheme,匹配host多個,彈出 Intent Chooser

3,同理,我們如果有多個activity;

if 兩個都一樣:

<activity            android:name=".MyIntentFilter"            android:label="@string/app_name" >            <intent-filter>                <category android:name="android.intent.category.DEFAULT"></category>                <action android:name="android.intent.action.SENDTO"/>                <data android:scheme="intbird" android:host="com.intbird.soft"/>            </intent-filter>        </activity>        <activity            android:name=".MyIntentFilter2"            android:label="@string/app_name" >            <intent-filter>                <category android:name="android.intent.category.DEFAULT"/>                <action android:name="android.intent.action.SENDTO"/>                <data android:scheme="intbird"  android:host="com.intbird.soft" />            </intent-filter>        </activity>

So:

but: add path [ android:path=”/testing” ] to one,startActivity directly

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.