網站首頁 編程語言 正文
前言
日常的Android開發中,我們會用到IntentFilter的匹配規則。IntentFilter的主要規則分為action、category、data三個類別,只有完美匹配才能成功啟動目標Activity;
今天我們就來講解下;
一、Activity的調用模式
Activity的調用模式有兩種:顯式調用和隱式調用;
1、顯式調用
大多數情況下我們最常接觸到的就是顯式調用了:
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
其實嚴格來講,這個也不算是顯式調用,因為在顯式調用的意義中需要明確之處被啟動的對象的組件信息,包括包名和類名,這里并沒有之處包名:
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、隱式調用
需要Intent能匹配目標組件的IntentFilter中所設置的過濾信息.如果不匹配將無法啟動目標Activity;
Intent intent = new Intent(); intent.setAction("android.intent.action.View"); startActivity(intent);
二、IntentFilter匹配規則詳解
1、Action的匹配規則
- action是一個字符串,系統預定義了一些action,同時我們也可以在應用中定義自己的action;
- 它的匹配規則是Intent中的action必須能夠和過濾規則中的action匹配,這里說的是指action的字符串值完全一樣;
- action中的內容是區分大小寫的;
- Intent中如果沒有指定action,則視為匹配失敗;
- 一個過濾規則中有多個action,那么只要Intent中的action能夠和Activity過濾規則中的任何一個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"否則報錯 <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中點擊按鈕啟動B var intent = Intent() intent.action = "com.ysl.test" startActivity(intent) }
常見action如下(Intent類中的常量)
- Intent.ACTION_MAIN,標識 Activity 為一個程序的開始
- Intent.ACTION_VIEW,顯示用戶的數據
- Intent.ACTION_DIAL,用戶撥號面板
- Intent.ACTION_SENDTO,發送消息
- Intent.ACTION_PICK,從列表中選擇信息,一般用于選擇聯系人或者圖片等
- Intent.ACTION_ANSWER,處理呼入的電話
- Intent.ACTION_CHOOSER,顯示一個Activity選擇器,比如常見的選擇分享到哪里
2、category的匹配規則
category是一個字符串,系統預定義了一些category,同時我們也可以在應用中定義自己的category;
category的匹配規則是:
- Intent中可以沒有category,但是如果一旦有category,不管有幾個,每個都要能夠和過濾規則中的任何一個category匹配;
- 一個Intent可以設置多個category,且Intent中的所有category都必須匹配到Activity中;
- 也可以不設置category,這時系統會自動匹配android.intent.category.DEFAULT;
- 這里可能感覺和action很像,但是只要稍微注意一下就可以發現Intent是setAction和addCategory,也就是說action只有一個(注意是一個Intent只有一個action,但是一個Activity的intent-filter中可以有多個action),而category可以有很多個且所有的category都必須出現在Activity的category集中;
注意:
- 因為強制要求一個Activity需要一個,所以我們不用將這個categoty添加到intent中去匹配;
- 如果單獨只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的匹配規則
data的匹配規則:Intent中必須含有data數據,并且data數據能夠完全匹配過濾規則中的某一個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>]
具體的參數解釋:
- mimeType:指媒體類型,比如 image/jpeg、audio/mpeg4-generic、vidio/等,可以表示圖片、文本、視頻等不同的媒體格式;
- scheme:URI的模式,如http、file、content等,如果URI中沒有指定scheme,那么整個URI的其他參數無效,這也意味著URI是無效的;
- host:URI的主機名,如blog.csdn.net,如果host未指定,那么整個URI中的其他參數無效,這也意味著URI是無效的;
- port:URI中的端口號,比如80,進檔URI中指定了scheme和host參數的時候,port參數才是有意義的;
- path:表述路徑的完整信息;
- pathPrefix:表述路徑的前綴信息;
- pathPattern:表述路徑的完整信息,但它里面可以包含通配符 * ,表示0個或任意字符;
data的注意事項
- URI可以不設置,但如果設置了,則 scheme 和 host 屬性必須要設置;
- URI的 scheme屬性有默認值,默認值為content 或者 file,因此,就算在intent-filter 中沒有為data設置URI,也需要在匹配的時候設置scheme和host兩個屬性,且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總結
1、IntentFilter匹配優先級
查看Intent的過濾器(intent-filter),按照以下優先關系查找:action->data->category;
2、隱式intent;
每一個通過 startActivity() 方法發出的隱式 Intent 都至少有一個 category,就是 android.intent.category.DEFAULT,所以只要是想接收一個隱式 Intent 的 Activity 都應該包括 android.intent.category.DEFAULTcategory,不然將導致 Intent 匹配失敗
說一個activity組件要想被其他組件通過隱式intent調用, 則其在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>
總結
快年底了,大家要努力學習,可以找個好工作;
原文鏈接:https://juejin.cn/post/7035892608777846815
相關推薦
- 2022-06-01 Apache?Hudi靈活的Payload機制硬核解析_服務器其它
- 2022-10-06 Go語言實現常用排序算法的示例代碼_Golang
- 2022-03-19 解析SQL?Server?CDC配合Kafka?Connect監聽數據變化的問題_MsSql
- 2022-06-15 ASP.NET?MVC使用區域(Area)功能_基礎應用
- 2022-11-08 background-image 背景平鋪方式、 CSS3 background-size背景圖像大
- 2022-06-02 Apache?Hudi基于華米科技應用湖倉一體化改造_服務器其它
- 2024-03-17 bootloader配置權限過高會怎么樣?
- 2022-05-23 Android利用Palette實現提取圖片顏色_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支