intentFilter

當Intent在組件間傳遞時,組件如果想告知Android系統自己能夠響應和處理哪些Intent,那麼就需要用到IntentFilter對象。

    顧名思義,IntentFilter對象負責過濾掉組件無法響應和處理的Intent,只將自己關心的Intent接收進來進行處理。 IntentFilter實行“白名單”管理,即只列出組件樂意接受的Intent,但IntentFilter只會過濾隱式Intent,顯式的Intent會直接傳送到目標組件。 Android組件可以有一個或多個IntentFilter,每個IntentFilter之間相互獨立,只需要其中一個驗證通過則可。除了用於過濾廣播的IntentFilter可以在代碼中創建外其他的IntentFilter必須在AndroidManifest.xml文件中進行聲明。

    IntentFilter中具有和Intent對應的用於過濾ActionDataCategory的字段,一個隱式Intent要想被一個組件處理,必須通過這三個環節的檢查。

       

       一:檢查 Action 儘管一個Intent只可以設置一個Action,但一個Intentfilter可以持有一個或多個Action用於過濾,到達的Intent只需要匹配其中一個Action即可。 深入思考:如果一個Intentfilter沒有設置Action的值,那麼,任何一個Intent都不會被通過;反之,如果一個Intent對象沒有設置Action值,那麼它能通過所有的Intentfilter的Action檢查。

        

       二:檢查 Data 同Action一樣,Intentfilter中的Data部分也可以是一個或者多個,而且可以沒有。每個Data包含的內容爲URL和數據類型,進行Data檢查時主要也是對這兩點進行比較,比較規則: 如果一個Intent對象沒有設置Data,只有Intentfilter也沒有設置Data時纔可通過檢查。 如果一個Intent對象包含URI,但不包含數據類型:僅當Intentfilter也不指定數據類型,同時它們的URI匹配,才能通過檢測。 如果一個Intent對象包含數據類型,但不包含URI:僅當Intentfilter也沒指定URL,而只包含數據類型且與Intent相同,才通過檢測。 如果一個Intent對象既包含URI,也包含數據類型(或數據類型能夠從URI推斷出),只有當其數據類型匹配Intentfilter中的數據類型,並且通過了URL檢查時,該Intent對象才能通過檢查。

   其中URL由四部分組成它有四個屬性scheme、host、port、path對應於URI的每個部分。

       例如:content://com.wjr.example1:121/files

       scheme部分:content

       host部分:com.wjr.example1

       port部分:121

       path部分:files

    host和port部分一起構成URI的憑據(authority),如果host沒有指定,那port也會被忽略。

    這四個屬性是可選的,但他們之間並不是完全獨立的。要讓authority有意義,scheme必須要指定。要讓path有意思,scheme和authority必須指定。 Intentfilter中的path可以使用通配符來匹配path字段,Intent和Intentfilter都可以用通配符來指定MIME類型。

    

     三:檢查 Category Intentfilter中可以設置多個Category,Intent中也可以含有多個Category,只有Intent中的所有Category都能匹配到Intentfilter中的Category,Intent才能通過檢查。也就是說,如果Intent中的Category集合是Intentfilter中Category的集合的子集時,Intent才能通過檢查。如果Intent中沒有設置Category,則它能通過所有Intentfilter的Category檢查。 如果一個Intent能夠通過不止一個組件的Intentfilter,用戶可能會被問那個組件被激活。如果沒有目標找到,會產生一個異常。

 

 

 

IntentFilter

應用程序的組件爲了告訴Android自己能響應、處理哪些隱式Intent請求,可以聲明一個甚至多個IntentFilter。每個 IntentFilter描述該組件所能響應Intent請求的能力——組件希望接收什麼類型的請求行爲,什麼類型的請求數據。比如之前請求網頁瀏覽器這 個例子中,網頁瀏覽器程序的IntentFilter就應該聲明它所希望接收的Intent Action是WEB_SEARCH_ACTION,以及與之相關的請求數據是網頁地址URI格式。

如何爲組件聲明自己的IntentFilter? 常見的方法是在AndroidManifest.xml文件中用屬性<Intent-Filter>描述組件的IntentFilter。

前面我們提到,隱式Intent和IntentFilter進行比較時的三要素是Intent的Action、Data以及Category。實際 上,一個隱式Intent請求要能夠傳遞給目標組件,必要通過這三個方面的檢查。如果任何一方面不匹配,Android都不會將該隱式Intent傳遞給 目標組件。接下來我們講解這三方面檢查的具體規則。

1.動作測試

<intent-filter>元素中可以包括子元素<action>,比如:

  1. <intent-filter> 
  2. <action android:name="com.example.project.SHOW_CURRENT" /> 
  3. <action android:name="com.example.project.SHOW_RECENT" /> 
  4. <action android:name="com.example.project.SHOW_PENDING" /> 
  5. </intent-filter> 

一條<intent-filter>元素至少應該包含一個<action>,否則任何Intent請求都不能和該<intent-filter>匹配。

如果Intent請求的Action和<intent-filter>中個某一條<action>匹配,那麼該Intent就通
過了這條<intent-filter>的動作測試。

如果Intent請求或<intent-filter>中沒有說明具體的Action類型,那麼會出現下面兩種情況。

(1) 如果<intent-filter>中沒有包含任何Action類型,那麼無論什麼Intent請求都無法和這條<intent-filter>匹配。

(2) 反之,如果Intent請求中沒有設定Action類型,那麼只要<intent-filter>中包含有Action類型,這個Intent請求就將順利地通過<intent-filter>的行爲測試。

2.類別測試

<intent-filter>元素可以包含<category>子元素,比如:

  1. <intent-filter . . . > 
  2. <category android:name="android.Intent.Category.DEFAULT" /> 
  3. <category android:name="android.Intent.Category.BROWSABLE" /> 
  4. </intent-filter> 

只有當Intent請求中所有的Category與組件中某一個IntentFilter 的<category>完全匹配時,纔會讓該Intent請求通過測試,IntentFilter中多餘的<category> 聲明並不會導致匹配失敗。一個沒有指定任何類別測試的IntentFilter僅僅只會匹配沒有設置類別的Intent請求。

3.數據測試

數據在<intent-filter>中的描述如下:

  1. <intent-filter . . . > 
  2. <data android:type="video/mpeg" android:scheme="http" . . . /> 
  3. <data android:type="audio/mpeg" android:scheme="http" . . . /> 
  4. </intent-filter> 

<data>元素指定了希望接受的Intent請求的數據URI和數據類 型,URI被分成三部分來進行匹配:scheme、authority和path。其中,用setData()設定的Intent請求的URI數據類型和 scheme必須與IntentFilter中所指定的一致。若IntentFilter中還指定了authority或path,它們也需要相匹配纔會 通過測試。

講解完Intent基本概念之後,接下來我們就使用Intent激活Android自帶的電話撥號程序。通過這個實例你會發現,使用Intent並不像其概念描述得那樣難。

 

IntentFilter 
簡述:結構化描述intent匹配的信息。包含:action,categories and data(via type,scheme ,path),還有priority, to order multiple matching filters.
       IntentFilter 中如果action爲空,則視爲通配符,如果type爲空,則intent必須不設type,否則匹配不上。 
       data被分爲3個屬性:type,scheme,authority/path 任何設置的屬性intent必須匹配上。 
                           設置了scheme 而沒設type,則intent也必須類似,不能設置type,也不能是content: URI.
                           設置了type而沒設scheme:將匹配上沒有URI的intent,或者content:,file:的uri。
                           設置了authority:必須指定一個或多個相關聯的schemes 
                           設置了path:唏噓指定一個或多個相關聯的schemes 
       匹配規則: 
           IntentFilter 匹配Intent的上的條件: 
           Action : 值相同 ,或則IntentFilter未指定action. 
           DataType:. 系統通過調用Intent.resolve(ContentResolver)獲取type,通配符* 
                     在Intent/IntentFilter的MIME type中使用,區分大小寫 
           DataScheme:系統通過調用Intent. getData() and Uri.getScheme())獲取scheme, 區分大小寫
           DataAuthority:必須有一個dataScheme匹配上且authority值匹配上,或者IntentFilter沒有定義。Intent. getData() and Uri.getAuthority()獲取authority.
           DataPath: scheme and authority必須先匹配上 ntent. getData() and Uri.getPath(),獲取. 或者IntentFilter沒有定義
           Categories:all of the categories in the Intent match categories given in the filter 多餘的Categorie,不影響intent匹配,如果IntentFilter
                  沒有指定Categorie,則只能匹配上沒有Categorie的intent。 

常用intent列表: 
Android Intent 用法彙總 
顯示網頁 
- <activity android:name="BrowserActivity" android:label="Browser" android:launchMode="singleTask" android:alwaysRetainTaskState="true" android:configChanges="orientation|keyboardHidden" android:theme="@style/BrowserTheme">
- <!-- 
For these schemes were not particular MIME type has been 
                 supplied, we are a good candidate. 
  --> 
- <intent-filter> 
  <action android:name="android.intent.action.VIEW" /> 
  <category android:name="android.intent.category.DEFAULT" /> 
  <category android:name="android.intent.category.BROWSABLE" /> 
  <data android:scheme="http" /> 
  <data android:scheme="https" /> 
  <data android:scheme="about" /> 
  </intent-filter> 
- <!-- 
  For these schemes where any of these particular MIME types 
                  have been supplied, we are a good candidate. 
  --> 
- <intent-filter> 
  <action android:name="android.intent.action.VIEW" /> 
  <category android:name="android.intent.category.BROWSABLE" /> 
  <category android:name="android.intent.category.DEFAULT" /> 
  <data android:scheme="http" /> 
  <data android:scheme="https" /> 
  <data android:mimeType="text/html" /> 
  <data android:mimeType="text/plain" /> 
  <data android:mimeType="application/xhtml+xml" /> 
  <data android:mimeType="application/vnd.wap.xhtml+xml" /> 
  </intent-filter> 
- <!-- 
We are also the main entry point of the browser. 
  --> 
- <intent-filter> 
  <action android:name="android.intent.action.MAIN" /> 
  <category android:name="android.intent.category.DEFAULT" /> 
  <category android:name="android.intent.category.LAUNCHER" /> 
  <category android:name="android.intent.category.BROWSABLE" /> 
  </intent-filter> 
- <intent-filter> 
  <action android:name="android.intent.action.WEB_SEARCH" /> 
  <category android:name="android.intent.category.DEFAULT" /> 
  <category android:name="android.intent.category.BROWSABLE" /> 
  <data android:scheme="" /> 
  <data android:scheme="http" /> 
  <data android:scheme="https" /> 
  </intent-filter> 
- <intent-filter> 
  <action android:name="android.intent.action.WEB_SEARCH" /> 
  <action android:name="android.intent.action.MEDIA_SEARCH" /> 
  <category android:name="android.intent.category.DEFAULT" /> 
  </intent-filter> 
- <intent-filter> 
  <action android:name="android.intent.action.SEARCH" /> 
  <category android:name="android.intent.category.DEFAULT" /> 
  </intent-filter> 
  <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
- <intent-filter> 
  <action android:name="android.net.http.NETWORK_STATE" /> 
  <action android:name="android.intent.action.PROXY_CHANGE" /> 
  </intent-filter> 
  </activity> 

1. Uri uri = Uri.parse("http://google.com");   
   2. Intent it = new Intent(Intent.ACTION_VIEW, uri);   
   3. startActivity(it);  

顯示地圖 

   1. Uri uri = Uri.parse("geo:38.899533,-77.036476");   
   2. Intent it = new Intent(Intent.ACTION_VIEW, uri);    
   3. startActivity(it);    
   4. //其他 geo URI 範例   
   5. //geo:latitude,longitude   
   6. //geo:latitude,longitude?z=zoom   
   7. //geo:0,0?q=my+street+address   
   8. //geo:0,0?q=business+near+city   
   9. //google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom  



路徑規劃 

   1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");  
   2. Intent it = new Intent(Intent.ACTION_VIEW, uri);   
   3. startActivity(it);   
   4. //where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456  


打電話 
   1. //叫出撥號程序  
   2. Uri uri = Uri.parse("tel:0800000123");   
   3. Intent it = new Intent(Intent.ACTION_DIAL, uri);   
   4. startActivity(it);   

   1. //直接打電話出去   
   2. Uri uri = Uri.parse("tel:0800000123");   
   3. Intent it = new Intent(Intent.ACTION_CALL, uri);   
   4. startActivity(it);   
   5. //用這個,要在 AndroidManifest.xml 中,加上   
   6. //<uses-permission id="android.permission.CALL_PHONE" />   

傳送SMS/MMS 
   1. //調用短信程序  
   2. Intent it = new Intent(Intent.ACTION_VIEW, uri);   
   3. it.putExtra("sms_body", "The SMS text");    
   4. it.setType("vnd.android-dir/mms-sms");   
   5. startActivity(it);  

   1. //傳送消息  
   2. Uri uri = Uri.parse("smsto://0800000123");   
   3. Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
   4. it.putExtra("sms_body", "The SMS text");   
   5. startActivity(it);  

   1. //傳送 MMS   
   2. Uri uri = Uri.parse("content://media/external/images/media/23");   
   3. Intent it = new Intent(Intent.ACTION_SEND);    
   4. it.putExtra("sms_body", "some text");    
   5. it.putExtra(Intent.EXTRA_STREAM, uri);   
   6. it.setType("image/png");    
   7. startActivity(it);   

傳送 Email 
   1. Uri uri = Uri.parse("mailto:[email protected]");   
   2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
   3. startActivity(it);   

   1. Intent it = new Intent(Intent.ACTION_SEND);   
   2. it.putExtra(Intent.EXTRA_EMAIL, "[email protected]");   
   3. it.putExtra(Intent.EXTRA_TEXT, "The email body text");   
   4. it.setType("text/plain");   
   5. startActivity(Intent.createChooser(it, "Choose Email Client"));   

   1. Intent it=new Intent(Intent.ACTION_SEND);     
   2. String[] tos={"[email protected]"};     
   3. String[] ccs={"[email protected]"};     
   4. it.putExtra(Intent.EXTRA_EMAIL, tos);     
   5. it.putExtra(Intent.EXTRA_CC, ccs);     
   6. it.putExtra(Intent.EXTRA_TEXT, "The email body text");     
   7. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");     
   8. it.setType("message/rfc822");     
   9. startActivity(Intent.createChooser(it, "Choose Email Client"));  

   1. //傳送附件 
   2. Intent it = new Intent(Intent.ACTION_SEND);   
   3. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");   
   4. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");   
   5. sendIntent.setType("audio/mp3");   
   6. startActivity(Intent.createChooser(it, "Choose Email Client"));  

播放多媒體 
       Uri uri = Uri.parse("file:///sdcard/song.mp3");   
       Intent it = new Intent(Intent.ACTION_VIEW, uri);   
       it.setType("audio/mp3");   
       startActivity(it);  



       Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");  
       Intent it = new Intent(Intent.ACTION_VIEW, uri);   

       startActivity(it);  



Market 相關 
1.        //尋找某個應用  
2.        Uri uri = Uri.parse("market://search?q=pname:pkg_name");  
3.        Intent it = new Intent(Intent.ACTION_VIEW, uri);   
4.        startActivity(it);   
5.        //where pkg_name is the full package path for an application  

1.        //顯示某個應用的相關信息  
2.        Uri uri = Uri.parse("market://details?id=app_id");   
3.        Intent it = new Intent(Intent.ACTION_VIEW, uri);  
4.        startActivity(it);   
5.        //where app_id is the application ID, find the ID    
6.        //by clicking on your application on Market home    
7.        //page, and notice the ID from the address bar  


Uninstall 應用程序 
1.        Uri uri = Uri.fromParts("package", strPackageName, null);  
2.        Intent it = new Intent(Intent.ACTION_DELETE, uri);    
3.        startActivity(it);   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章