日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳細分析IntentFilter的匹配規(guī)則

前言

日常的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中所設(shè)置的過濾信息.如果不匹配將無法啟動目標Activity;

 
 
 
 
  1. Intent intent = new Intent(); 
  2. intent.setAction("android.intent.action.View"); 
  3. startActivity(intent);

二、IntentFilter匹配規(guī)則詳解

1、Action的匹配規(guī)則

  • action是一個字符串,系統(tǒng)預(yù)定義了一些action,同時我們也可以在應(yīng)用中定義自己的action;
  • 它的匹配規(guī)則是Intent中的action必須能夠和過濾規(guī)則中的action匹配,這里說的是指action的字符串值完全一樣;
  • action中的內(nèi)容是區(qū)分大小寫的;
  • Intent中如果沒有指定action,則視為匹配失敗;
  • 一個過濾規(guī)則中有多個action,那么只要Intent中的action能夠和Activity過濾規(guī)則中的任何一個action相同即可匹配成功;
 
 
 
 
  1.  
  2.             
  3.                 
  4.                 
  5.                 //必須添加category android:name="android.intent.category.DEFAULT"否則報錯
  6.                 
  7.             
  8.         
  9.         
  10.             
  11.                 
  12.                 
  13.             
  14.         
  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)預(yù)定義了一些category,同時我們也可以在應(yīng)用中定義自己的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之后才行;
 
 
 
 
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13. Intent intent = new Intent();
  14. intent.addCategory("com.yu.hu.category1");
  15. intent.addCategory("com.yu.hu.category2");
  16. intent.setAction("com.yu.hu.what");
  17. startActivity(intent);

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

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

data的語法格式

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

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

 
 
 
 
  1. ://:/[||]

具體的參數(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;
 
 
 
 
  1.    
  2.    
  3.    
  4.      android:host="www.baidu.com"
  5.      android:pathPrefix="/imgs"
  6.      android:port="8080"
  7.      android:scheme="https" />
  8.  
  9.   Intent intent = new Intent();
  10.   intent.setData(Uri.parse("https://www.baidu.com:8080/imgs/img1.png"));
  11.   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 都應(yīng)該包括 android.intent.category.DEFAULTcategory,不然將導(dǎo)致 Intent 匹配失敗

說一個activity組件要想被其他組件通過隱式intent調(diào)用, 則其在AndroiddManifest.xml中的聲明如下:

 
 
 
 
  1.        
  2.            
  3.            
  4.   

總結(jié)

快年底了,大家要努力學(xué)習(xí),可以找個好工作;

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


本文標題:詳細分析IntentFilter的匹配規(guī)則
當前網(wǎng)址:http://www.5511xx.com/article/dhdpjhd.html