intent intent-filter intent filer

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

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

1.動作測試

<intent-filter>元素中可以包括子元素<action>,比如:
<intent-filter>
<action android:name=”com.example.project.SHOW_CURRENT” />
<action android:name=”com.example.project.SHOW_RECENT” />
<action android:name=”com.example.project.SHOW_PENDING” />
</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>子元素,比如:
<intent-filter . . . >
<category android:name=”android.Intent.Category.DEFAULT” />
<category android:name=”android.Intent.Category.BROWSABLE” />
</intent-filter>

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

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

<intent-filter . . . >
<data android:type=”video/mpeg” android:scheme=”http” . . . />
<data android:type=”audio/mpeg” android:scheme=”http” . . . />
</intent-filter>

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

講解完Intent基本概念之後,接下來我們就使用Intent激活Android自帶的電話撥號程序,通過這個實例你會發現,使用Intent並不像其概念描述得那樣難。最終創建Intent的代碼如下所示。
Intent i = new Intent(Intent.ACTION_DIAL,Uri.parse(”tel://13800138000″));
創建好Intent之後,你就可以通過它告訴Android希望啓動新的Activity了。
startActivity(i);
Activity啓動後顯示界面如下:
[img]http://dl.iteye.com/upload/attachment/259083/7c57cfb1-657a-30c4-a3c1-c0d3aea47d2d.jpg[/img]

5.總結說明

這篇文章是我剛開始學習Android時看到的,當時理解的不是很深入,現在再回頭看這篇文章總結的很詳細,在這裏與大家分享。


1,掉web瀏覽器


Uri myBlogUri = Uri.parse("http://kuikui.iteye.com");

returnIt = new Intent(Intent.ACTION_VIEW, myBlogUri);



2,地圖

Uri mapUri = Uri.parse("geo:38.899533,-77.036476");

returnIt = new Intent(Intent.ACTION_VIEW, mapUri);



3,調撥打電話界面

Uri telUri = Uri.parse("tel:100861");

returnIt = new Intent(Intent.ACTION_DIAL, telUri);



4,直接撥打電話

Uri callUri = Uri.parse("tel:100861");

returnIt = new Intent(Intent.ACTION_CALL, callUri);



5,卸載

Uri uninstallUri = Uri.fromParts("package", "xxx", null);

returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);



6,安裝

Uri installUri = Uri.fromParts("package", "xxx", null);

returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);



7,播放

Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3"); 

returnIt = new Intent(Intent.ACTION_VIEW, playUri);



8,掉用發郵件

Uri emailUri = Uri.parse("mailto:[email protected]");

returnIt = new Intent(Intent.ACTION_SENDTO, emailUri);



9,發郵件

returnIt = new Intent(Intent.ACTION_SEND);

String[] tos = { "[email protected]" };

String[] ccs = { "[email protected]" };

returnIt.putExtra(Intent.EXTRA_EMAIL, tos);

returnIt.putExtra(Intent.EXTRA_CC, ccs);

returnIt.putExtra(Intent.EXTRA_TEXT, "body");

returnIt.putExtra(Intent.EXTRA_SUBJECT, "subject");

returnIt.setType("message/rfc882");

Intent.createChooser(returnIt, "Choose Email Client");



10,發短信

Uri smsUri = Uri.parse("tel:100861"); 

returnIt = new Intent(Intent.ACTION_VIEW, smsUri);

returnIt.putExtra("sms_body", "shenrenkui");

returnIt.setType("vnd.android-dir/mms-sms");



11,直接發郵件

Uri smsToUri = Uri.parse("smsto://100861"); 

returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri);

returnIt.putExtra("sms_body", "shenrenkui");



12,發彩信

Uri mmsUri = Uri.parse("content://media/external/images/media/23"); 

returnIt = new Intent(Intent.ACTION_SEND);

returnIt.putExtra("sms_body", "shenrenkui");

returnIt.putExtra(Intent.EXTRA_STREAM, mmsUri);

returnIt.setType("image/png");



用獲取到的Intent直接調用startActivity(returnIt)就ok了。
發佈了43 篇原創文章 · 獲贊 0 · 訪問量 2667
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章