成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

詳細分析IntentFilter的匹配規(guī)則

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

[[430933]]

前言

日常的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)用了:

  1. Intent intent = new Intent(FirstActivity.this,SecondActivity.class);  
  2. startActivity(intent); 

其實嚴格來講,這個也不算是顯式調(diào)用,因為在顯式調(diào)用的意義中需要明確之處被啟動的對象的組件信息,包括包名和類名,這里并沒有之處包名:

  1. Intent intent = new Intent(Intent.ACTION_MAIN); 
  2. intent.addCategory(Intent.CATEGORY_LAUNCHER); 
  3. ComponentName cn = new ComponentName("com.test","com.test.MainActivity"); 
  4. intent.setComponent(cn); 
  5. startActivity(intent); 

2、隱式調(diào)用

需要Intent能匹配目標組件的IntentFilter中所設置的過濾信息.如果不匹配將無法啟動目標Activity;

  1. Intent intent = new Intent();  
  2. intent.setAction("android.intent.action.View");  
  3. 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相同即可匹配成功;

  1.  <activity android:name=".BActivity" > 
  2.             <intent-filter> 
  3.                 <action android:name="com.ysl.test"/> 
  4.                 <action android:name="com.ysl.test1"/> 
  5.                 //必須添加category android:name="android.intent.category.DEFAULT"否則報錯 
  6.                 <category android:name="android.intent.category.DEFAULT"/> 
  7.             </intent-filter> 
  8.         </activity> 
  9.         <activity android:name=".AActivity" > 
  10.             <intent-filter> 
  11.                 <action android:name="android.intent.action.MAIN" /> 
  12.                 <category android:name="android.intent.category.LAUNCHER" /> 
  13.             </intent-filter> 
  14.         </activity> 
  15. btn_skip_b.setOnClickListener { 
  16.             //A中點擊按鈕啟動B 
  17.             var intent = Intent() 
  18.             intent.action = "com.ysl.test" 
  19.             startActivity(intent) 
  20.         } 

常見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可以設置多個category,且Intent中的所有category都必須匹配到Activity中;
  • 也可以不設置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之后才行;

  1. <!--SecondActivity的intent-filter--> 
  2. <intent-filter> 
  3.  <action android:name="com.axe.mg.what" /> 
  4.  <category android:name="com.yu.hu.category1"/> 
  5.  <category android:name="com.yu.hu.category2"/> 
  6.  <category android:name = "android.intent.category.DEFAULT" /> 
  7. </intent-filter> 
  8. <!--ThirdActivity的intent-filter--> 
  9. <intent-filter> 
  10.  <action android:name="com.axe.mg.what" /> 
  11.  <category android:name = "android.intent.category.DEFAULT" /> 
  12.  <category android:name="com.yu.hu.category1"/> 
  13.  <category android:name="com.yu.hu.category2"/> 
  14.  <category android:name="com.yu.hu.category3"/> 
  15. </intent-filter> 
  16. <!--FourthActivity的intent-filter--> 
  17. <intent-filter> 
  18.  <action android:name="com.axe.mg.what" /> 
  19.  <category android:name = "android.intent.category.DEFAULT" /> 
  20.  <category android:name="com.yu.hu.category2"/> 
  21. </intent-filter> 
  22. Intent intent = new Intent(); 
  23. intent.addCategory("com.yu.hu.category1"); 
  24. intent.addCategory("com.yu.hu.category2"); 
  25. intent.setAction("com.yu.hu.what"); 
  26. startActivity(intent); 

3、data的匹配規(guī)則

data的匹配規(guī)則:Intent中必須含有data數(shù)據(jù),并且data數(shù)據(jù)能夠完全匹配過濾規(guī)則中的某一個data;

data的語法格式

  1. <data android:scheme="string" 
  2.     android:host="string" 
  3.     android:port="string" 
  4.     android:path="string" 
  5.     android:pathPattern="string" 
  6.     android:pathPrefix="string" 
  7.     android:mimeType="string" /> 

data由兩部分組成: mimeType和 URI,URI通過如下格式,包括scheme、host、port、path、pathPrefix和pathPattern;

  1. <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可以不設置,但如果設置了,則 scheme 和 host 屬性必須要設置;
  • URI的 scheme屬性有默認值,默認值為content 或者 file,因此,就算在intent-filter 中沒有為data設置URI,也需要在匹配的時候設置scheme和host兩個屬性,且scheme屬性的值必須是content或者file;

  1. <intent-filter> 
  2.    <action android:name="xx" /> 
  3.    <category android:name="android.intent.category.DEFAULT" /> 
  4.    <data 
  5.      android:host="www.baidu.com" 
  6.      android:pathPrefix="/imgs" 
  7.      android:port="8080" 
  8.      android:scheme="https" /> 
  9.  </intent-filter> 
  10.   Intent intent = new Intent(); 
  11.   intent.setData(Uri.parse("https://www.baidu.com:8080/imgs/img1.png")); 
  12.   startActivity(intent); 

三、IntentFilter總結(jié)

1、IntentFilter匹配優(yōu)先級

查看Intent的過濾器(intent-filter),按照以下優(yōu)先關系查找: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中的聲明如下:

  1. <activity android:name="com..test.MainActivity"
  2.      <intent-filter>   
  3.            <action android:name="com.test.test" /> 
  4.            <category android:name="android.intent.category.DEFAULT" /> 
  5.  </intent-filter>  
  6. </activity> 

總結(jié)

快年底了,大家要努力學習,可以找個好工作;

本文轉(zhuǎn)載自微信公眾號「Android開發(fā)編程」

 

責任編輯:姜華 來源: Android開發(fā)編程
相關推薦

2009-06-18 14:00:51

2009-09-25 14:23:39

2009-09-28 10:39:01

Hibernate基礎

2009-12-03 17:41:40

PHP應用發(fā)展

2009-09-14 13:50:35

LINQ編程模型

2009-09-08 15:56:50

Linq使用Group

2010-01-06 13:50:37

.NET Framew

2009-11-20 13:11:44

Oracle XML數(shù)

2010-04-26 18:17:19

Oracle存儲過程

2009-09-09 09:48:43

Linq延遲加載

2009-09-14 16:21:34

LINQ To XML

2009-10-10 13:52:57

VB Update方法

2010-03-08 17:18:46

Linux du命令

2010-04-12 10:53:07

Oracle SQL

2010-04-26 14:32:21

Oracle SQL

2010-07-02 11:19:31

IP協(xié)議頭格式

2009-09-07 14:18:01

C#內(nèi)存管理

2009-09-09 13:53:21

Linq表值函數(shù)

2009-03-24 09:17:58

驅(qū)動GSMAndroid

2009-10-28 10:06:29

VB.NET With
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 国产免费av在线 | 国产高清自拍视频在线观看 | 久久亚洲天堂 | 99草免费视频 | 国产一级视频在线播放 | 人人操日日干 | 国产精品毛片在线 | 久久se精品一区精品二区 | 中文字幕在线观看成人 | 亚洲成年影院 | 亚洲免费久久久 | 婷婷综合色 | 请别相信他免费喜剧电影在线观看 | 欧美一区二区三区电影 | 欧美综合自拍 | 91在线视频免费观看 | 成人午夜在线视频 | 精品区| 五月激情久久 | 欧美精品久久久 | 一区二区高清在线观看 | 久久久久久久久久久久久九 | 野狼在线社区2017入口 | 99国产精品久久久久久久 | 午夜精品一区二区三区三上悠亚 | 综合色在线 | 北条麻妃99精品青青久久主播 | 精品久久久久久 | 欧美日韩在线播放 | 久久99深爱久久99精品 | 一区二区三区国产好的精 | 3级毛片 | 欧美自拍一区 | 国产一区二区观看 | 天天射天天操天天干 | 亚洲一区二区三区视频 | 久久免费视频网 | 午夜午夜精品一区二区三区文 | 日韩欧美专区 | 在线精品国产 | 国产精品久久国产精品 |