新聞中心
前言
日常的Android開發(fā)中,我們會用到IntentFilter的匹配規(guī)則。IntentFilter的主要規(guī)則分為action、category、data三個類別,只有完美匹配才能成功啟動目標Activity;

今天我們就來講解下;
一、Activity的調(diào)用模式
Activity的調(diào)用模式有兩種:顯式調(diào)用和隱式調(diào)用;
1、顯式調(diào)用
大多數(shù)情況下我們最常接觸到的就是顯式調(diào)用了:
- Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
- startActivity(intent);
其實嚴格來講,這個也不算是顯式調(diào)用,因為在顯式調(diào)用的意義中需要明確之處被啟動的對象的組件信息,包括包名和類名,這里并沒有之處包名:
- Intent intent = new Intent(Intent.ACTION_MAIN);
- intent.addCategory(Intent.CATEGORY_LAUNCHER);
- ComponentName cn = new ComponentName("com.test","com.test.MainActivity");
- intent.setComponent(cn);
- startActivity(intent);
2、隱式調(diào)用
需要Intent能匹配目標組件的IntentFilter中所設(shè)置的過濾信息.如果不匹配將無法啟動目標Activity;
- Intent intent = new Intent();
- intent.setAction("android.intent.action.View");
- startActivity(intent);
二、IntentFilter匹配規(guī)則詳解
1、Action的匹配規(guī)則
- action是一個字符串,系統(tǒng)預定義了一些action,同時我們也可以在應用中定義自己的action;
- 它的匹配規(guī)則是Intent中的action必須能夠和過濾規(guī)則中的action匹配,這里說的是指action的字符串值完全一樣;
- action中的內(nèi)容是區(qū)分大小寫的;
- Intent中如果沒有指定action,則視為匹配失敗;
- 一個過濾規(guī)則中有多個action,那么只要Intent中的action能夠和Activity過濾規(guī)則中的任何一個action相同即可匹配成功;
- //必須添加category android:name="android.intent.category.DEFAULT"否則報錯
- btn_skip_b.setOnClickListener {
- //A中點擊按鈕啟動B
- var intent = Intent()
- intent.action = "com.ysl.test"
- startActivity(intent)
- }
常見action如下(Intent類中的常量)
- Intent.ACTION_MAIN,標識 Activity 為一個程序的開始
- Intent.ACTION_VIEW,顯示用戶的數(shù)據(jù)
- Intent.ACTION_DIAL,用戶撥號面板
- Intent.ACTION_SENDTO,發(fā)送消息
- Intent.ACTION_PICK,從列表中選擇信息,一般用于選擇聯(lián)系人或者圖片等
- Intent.ACTION_ANSWER,處理呼入的電話
- Intent.ACTION_CHOOSER,顯示一個Activity選擇器,比如常見的選擇分享到哪里
2、category的匹配規(guī)則
category是一個字符串,系統(tǒng)預定義了一些category,同時我們也可以在應用中定義自己的category;
category的匹配規(guī)則是:
- Intent中可以沒有category,但是如果一旦有category,不管有幾個,每個都要能夠和過濾規(guī)則中的任何一個category匹配;
- 一個Intent可以設(shè)置多個category,且Intent中的所有category都必須匹配到Activity中;
- 也可以不設(shè)置category,這時系統(tǒng)會自動匹配android.intent.category.DEFAULT;
- 這里可能感覺和action很像,但是只要稍微注意一下就可以發(fā)現(xiàn)Intent是setAction和addCategory,也就是說action只有一個(注意是一個Intent只有一個action,但是一個Activity的intent-filter中可以有多個action),而category可以有很多個且所有的category都必須出現(xiàn)在Activity的category集中;
注意:
- 因為強制要求一個Activity需要一個 ,所以我們不用將這個categoty添加到intent中去匹配;
- 如果單獨只addCategory是沒有用的,必須setAction之后才行;
- Intent intent = new Intent();
- intent.addCategory("com.yu.hu.category1");
- intent.addCategory("com.yu.hu.category2");
- intent.setAction("com.yu.hu.what");
- startActivity(intent);
3、data的匹配規(guī)則
data的匹配規(guī)則:Intent中必須含有data數(shù)據(jù),并且data數(shù)據(jù)能夠完全匹配過濾規(guī)則中的某一個data;
data的語法格式
- android:host="string"
- android:port="string"
- android:path="string"
- android:pathPattern="string"
- android:pathPrefix="string"
- android:mimeType="string" />
data由兩部分組成: mimeType和 URI,URI通過如下格式,包括scheme、host、port、path、pathPrefix和pathPattern;
:// : /[ | | ]
具體的參數(shù)解釋:
- mimeType:指媒體類型,比如 image/jpeg、audio/mpeg4-generic、vidio/等,可以表示圖片、文本、視頻等不同的媒體格式;
- scheme:URI的模式,如http、file、content等,如果URI中沒有指定scheme,那么整個URI的其他參數(shù)無效,這也意味著URI是無效的;
- host:URI的主機名,如blog.csdn.net,如果host未指定,那么整個URI中的其他參數(shù)無效,這也意味著URI是無效的;
- port:URI中的端口號,比如80,進檔URI中指定了scheme和host參數(shù)的時候,port參數(shù)才是有意義的;
- path:表述路徑的完整信息;
- pathPrefix:表述路徑的前綴信息;
- pathPattern:表述路徑的完整信息,但它里面可以包含通配符 * ,表示0個或任意字符;
data的注意事項
- URI可以不設(shè)置,但如果設(shè)置了,則 scheme 和 host 屬性必須要設(shè)置;
- URI的 scheme屬性有默認值,默認值為content 或者 file,因此,就算在intent-filter 中沒有為data設(shè)置URI,也需要在匹配的時候設(shè)置scheme和host兩個屬性,且scheme屬性的值必須是content或者file;
- android:host="www.baidu.com"
- android:pathPrefix="/imgs"
- android:port="8080"
- android:scheme="https" />
- Intent intent = new Intent();
- intent.setData(Uri.parse("https://www.baidu.com:8080/imgs/img1.png"));
- startActivity(intent);
三、IntentFilter總結(jié)
1、IntentFilter匹配優(yōu)先級
查看Intent的過濾器(intent-filter),按照以下優(yōu)先關(guān)系查找:action->data->category;
2、隱式intent;
每一個通過 startActivity() 方法發(fā)出的隱式 Intent 都至少有一個 category,就是 android.intent.category.DEFAULT,所以只要是想接收一個隱式 Intent 的 Activity 都應該包括 android.intent.category.DEFAULTcategory,不然將導致 Intent 匹配失敗
說一個activity組件要想被其他組件通過隱式intent調(diào)用, 則其在AndroiddManifest.xml中的聲明如下:
總結(jié)
快年底了,大家要努力學習,可以找個好工作;
本文轉(zhuǎn)載自微信公眾號「Android開發(fā)編程」
文章名稱:詳細分析IntentFilter的匹配規(guī)則
文章鏈接:http://m.fisionsoft.com.cn/article/dhdpjhd.html


咨詢
建站咨詢
