網(wǎng)站首頁(yè) 編程語言 正文
前言
日常的Android開發(fā)中,我們會(huì)用到IntentFilter的匹配規(guī)則。IntentFilter的主要規(guī)則分為action、category、data三個(gè)類別,只有完美匹配才能成功啟動(dòng)目標(biāo)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);
其實(shí)嚴(yán)格來講,這個(gè)也不算是顯式調(diào)用,因?yàn)樵陲@式調(diào)用的意義中需要明確之處被啟動(dòng)的對(duì)象的組件信息,包括包名和類名,這里并沒有之處包名:
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能匹配目標(biāo)組件的IntentFilter中所設(shè)置的過濾信息.如果不匹配將無法啟動(dòng)目標(biāo)Activity;
Intent intent = new Intent(); intent.setAction("android.intent.action.View"); startActivity(intent);
二、IntentFilter匹配規(guī)則詳解
1、Action的匹配規(guī)則
- action是一個(gè)字符串,系統(tǒng)預(yù)定義了一些action,同時(shí)我們也可以在應(yīng)用中定義自己的action;
- 它的匹配規(guī)則是Intent中的action必須能夠和過濾規(guī)則中的action匹配,這里說的是指action的字符串值完全一樣;
- action中的內(nèi)容是區(qū)分大小寫的;
- Intent中如果沒有指定action,則視為匹配失敗;
- 一個(gè)過濾規(guī)則中有多個(gè)action,那么只要Intent中的action能夠和Activity過濾規(guī)則中的任何一個(gè)action相同即可匹配成功;
<activity android:name=".BActivity" > <intent-filter> <action android:name="com.ysl.test"/> <action android:name="com.ysl.test1"/> //必須添加category android:name="android.intent.category.DEFAULT"否則報(bào)錯(cuò) <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> <activity android:name=".AActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> btn_skip_b.setOnClickListener { //A中點(diǎn)擊按鈕啟動(dòng)B var intent = Intent() intent.action = "com.ysl.test" startActivity(intent) }
常見action如下(Intent類中的常量)
- Intent.ACTION_MAIN,標(biāo)識(shí) Activity 為一個(gè)程序的開始
- Intent.ACTION_VIEW,顯示用戶的數(shù)據(jù)
- Intent.ACTION_DIAL,用戶撥號(hào)面板
- Intent.ACTION_SENDTO,發(fā)送消息
- Intent.ACTION_PICK,從列表中選擇信息,一般用于選擇聯(lián)系人或者圖片等
- Intent.ACTION_ANSWER,處理呼入的電話
- Intent.ACTION_CHOOSER,顯示一個(gè)Activity選擇器,比如常見的選擇分享到哪里
2、category的匹配規(guī)則
category是一個(gè)字符串,系統(tǒng)預(yù)定義了一些category,同時(shí)我們也可以在應(yīng)用中定義自己的category;
category的匹配規(guī)則是:
- Intent中可以沒有category,但是如果一旦有category,不管有幾個(gè),每個(gè)都要能夠和過濾規(guī)則中的任何一個(gè)category匹配;
- 一個(gè)Intent可以設(shè)置多個(gè)category,且Intent中的所有category都必須匹配到Activity中;
- 也可以不設(shè)置category,這時(shí)系統(tǒng)會(huì)自動(dòng)匹配android.intent.category.DEFAULT;
- 這里可能感覺和action很像,但是只要稍微注意一下就可以發(fā)現(xiàn)Intent是setAction和addCategory,也就是說action只有一個(gè)(注意是一個(gè)Intent只有一個(gè)action,但是一個(gè)Activity的intent-filter中可以有多個(gè)action),而category可以有很多個(gè)且所有的category都必須出現(xiàn)在Activity的category集中;
注意:
- 因?yàn)閺?qiáng)制要求一個(gè)Activity需要一個(gè),所以我們不用將這個(gè)categoty添加到intent中去匹配;
- 如果單獨(dú)只addCategory是沒有用的,必須setAction之后才行;
<!--SecondActivity的intent-filter--> <intent-filter> <action android:name="com.axe.mg.what" /> <category android:name="com.yu.hu.category1"/> <category android:name="com.yu.hu.category2"/> <category android:name = "android.intent.category.DEFAULT" /> </intent-filter> <!--ThirdActivity的intent-filter--> <intent-filter> <action android:name="com.axe.mg.what" /> <category android:name = "android.intent.category.DEFAULT" /> <category android:name="com.yu.hu.category1"/> <category android:name="com.yu.hu.category2"/> <category android:name="com.yu.hu.category3"/> </intent-filter> <!--FourthActivity的intent-filter--> <intent-filter> <action android:name="com.axe.mg.what" /> <category android:name = "android.intent.category.DEFAULT" /> <category android:name="com.yu.hu.category2"/> </intent-filter> 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ī)則中的某一個(gè)data;
data的語法格式
<data android:scheme="string" 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;
<scheme>://<host>:<port>/[<path>|<pathPrefix>|<pathPattern>]
具體的參數(shù)解釋:
- mimeType:指媒體類型,比如 image/jpeg、audio/mpeg4-generic、vidio/等,可以表示圖片、文本、視頻等不同的媒體格式;
- scheme:URI的模式,如http、file、content等,如果URI中沒有指定scheme,那么整個(gè)URI的其他參數(shù)無效,這也意味著URI是無效的;
- host:URI的主機(jī)名,如blog.csdn.net,如果host未指定,那么整個(gè)URI中的其他參數(shù)無效,這也意味著URI是無效的;
- port:URI中的端口號(hào),比如80,進(jìn)檔URI中指定了scheme和host參數(shù)的時(shí)候,port參數(shù)才是有意義的;
- path:表述路徑的完整信息;
- pathPrefix:表述路徑的前綴信息;
- pathPattern:表述路徑的完整信息,但它里面可以包含通配符 * ,表示0個(gè)或任意字符;
data的注意事項(xiàng)
- URI可以不設(shè)置,但如果設(shè)置了,則 scheme 和 host 屬性必須要設(shè)置;
- URI的 scheme屬性有默認(rèn)值,默認(rèn)值為content 或者 file,因此,就算在intent-filter 中沒有為data設(shè)置URI,也需要在匹配的時(shí)候設(shè)置scheme和host兩個(gè)屬性,且scheme屬性的值必須是content或者file;
<intent-filter> <action android:name="xx" /> <category android:name="android.intent.category.DEFAULT" /> <data android:host="www.baidu.com" android:pathPrefix="/imgs" android:port="8080" android:scheme="https" /> </intent-filter> Intent intent = new Intent(); intent.setData(Uri.parse("https://www.baidu.com:8080/imgs/img1.png")); startActivity(intent);
三、IntentFilter總結(jié)
1、IntentFilter匹配優(yōu)先級(jí)
查看Intent的過濾器(intent-filter),按照以下優(yōu)先關(guān)系查找:action->data->category;
2、隱式intent;
每一個(gè)通過 startActivity() 方法發(fā)出的隱式 Intent 都至少有一個(gè) category,就是 android.intent.category.DEFAULT,所以只要是想接收一個(gè)隱式 Intent 的 Activity 都應(yīng)該包括 android.intent.category.DEFAULTcategory,不然將導(dǎo)致 Intent 匹配失敗
說一個(gè)activity組件要想被其他組件通過隱式intent調(diào)用, 則其在AndroiddManifest.xml中的聲明如下:
<activity android:name="com..test.MainActivity"> <intent-filter> <action android:name="com.test.test" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
總結(jié)
快年底了,大家要努力學(xué)習(xí),可以找個(gè)好工作;
原文鏈接:https://juejin.cn/post/7035892608777846815
相關(guān)推薦
- 2022-09-13 go開源項(xiàng)目用戶名密碼驗(yàn)證的邏輯鬼才寫法_Golang
- 2022-05-06 如何利用Go語言實(shí)現(xiàn)LRU?Cache_Golang
- 2022-05-20 利用PyQt5模擬實(shí)現(xiàn)網(wǎng)頁(yè)鼠標(biāo)移動(dòng)特效_python
- 2023-04-02 攔截信號(hào)Golang應(yīng)用優(yōu)雅關(guān)閉的操作方法_Golang
- 2022-10-13 C++智能指針詳解_C 語言
- 2023-03-27 詳解C++11中的類型推斷_C 語言
- 2022-05-18 Jenkins使用publish?html?report插件展示HTML報(bào)告的方法_相關(guān)技巧
- 2023-10-25 用原生promise特性替代async/await解決異步的方法
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支