應用程序的組件爲了告訴Android自己能響應、處理哪些隱式Intent請求,可以聲明一個甚至多個Intent Filter。每個Intent Filter描述該組件所能響應Intent請求的能力——組件希望接收什麼類型的請求行爲,什麼類型的請求數據。
1.Action Test
<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.Category Test
<intent-filter>元素可以包含<category>子元素,比如:
<intent-filter>
<category android:name=”android.Intent.Category.DEFAULT”/>
<category android:name=”android.Intent.Category.BROWSABLE”/>
</intent-filter>
只有當Intent請求中所有的Category與組件中某一個Intent Filter的<category>完全匹配時,纔會讓該
Intent請求通過測試,Intent Filter中多餘的<category>聲明並不會導致匹配失敗。一個沒有指定任何類別測試的
IntentFilter僅僅只會匹配沒有設置類別的Intent請求。
3.Data Test
數據在<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()設定的Intent請求的URI數據類型和scheme必須與Intent
Filter中所指定的一致。若IntentFilter中還指定了authority或path,它們也需要相匹配纔會通過測試。
4.Sample
說完Intent基本概念之後,接下來我們就使用Intent激活Android自帶的電話撥號程序,通過這個實例你會發現,使用Intent並不像其概念描述的那樣難。最終創建Intent的代碼如下所示:
Intent i = new Intent(Intent.ACTION_DIAL,Uri.parse(”tel://10086″));
//啓動新的Activity
startActivity(i);
新的Activity啓動後顯示界面如下:
5.Summary
(1).web browser
Uri myBlogUri = Uri.parse("http://blog.csdn.net/jackhenry");
returnIt = new Intent(Intent.ACTION_VIEW, myBlogUri);
(2).map
Uri mapUri = Uri.parse("geo:38.899533,-77.036476");
returnIt = new Intent(Intent.ACTION_VIEW, mapUri);
(3).dial
Uri telUri = Uri.parse("tel:10086");
returnIt = new Intent(Intent.ACTION_DIAL, telUri);
(4).call
Uri callUri = Uri.parse("tel:10086");
returnIt = new Intent(Intent.ACTION_CALL, callUri);
(5).uninstall
Uri uninstallUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);
(6).install
Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
(7).media player
Uri playUri = Uri.parse("file:///sdcard/download/beautiful.mp3");
returnIt = new Intent(Intent.ACTION_VIEW, playUri);
(8).send a message to someone specified by the data
Uri emailUri = Uri.parse("mailto:[email protected]");
returnIt = new Intent(Intent.ACTION_SENDTO, emailUri);
(9).send mail
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).send SMS
Uri smsUri = Uri.parse("tel:10086");
returnIt = new Intent(Intent.ACTION_VIEW, smsUri);
returnIt.putExtra("sms_body", "jackhenry");
returnIt.setType("vnd.android-dir/mms-sms");
(11).send mail directly
Uri smsToUri = Uri.parse("smsto://10086");
returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri);
returnIt.putExtra("sms_body", "jackhenry");
(12).send MMS
Uri mmsUri = Uri.parse("content://media/external/images/emniem");
returnIt = new Intent(Intent.ACTION_SEND);
returnIt.putExtra("sms_body", "jackhenry");
returnIt.putExtra(Intent.EXTRA_STREAM, mmsUri);
returnIt.setType("image/png");
用獲取到的Intent直接調用startActivity(returnIt)就ok了。
source address:http://dev.10086.cn/cmdn/wiki/index.php?doc-view-3466.html