顯式Intent定義:對於明確指出了目標組件名稱的Intent,我們稱之為顯式Intent。顯式Intent直接用組件的名稱定義目標組件,這種方式很直接。但是由於開發人員往往並不清楚別的應用程式的組件名稱,因此,顯式Intent更多用於在應用程式內部傳遞訊息。比如在某應用程式內,一個Activity啟動一個Service。
隱式Intent定義:對於沒有明確指出目標組件名稱的Intent,則稱之為隱式Intent。它不會用組件名稱定義需要啟用的目標組件,它更廣泛地用於在不同應用程式之間傳遞訊息.由於沒有明確的目標組件名稱,所以必須由Android系統輔助應用程式尋找與Intent請求意圖最匹配的組件。
隱式啟動Intent:
Android系統尋找與Intent請求意圖最匹配的組件具體的選擇方法是:
Android將Intent的請求內容和一個叫做IntentFilter的過濾器比較,IntentFilter中包含系統中所有可能的待選組件.如果IntentFilter中某一組件匹配隱式Intent請求的內容,那麼Android就選擇該組件作為該隱式Intent的目標組件。
Android如何知道應用程式能夠處理某種類型的Intent請求呢?這需要應用程式在Android-Manifest.xml中聲明自己所含組件的過濾器(即可以匹配哪些Intent請求)。
一個沒有聲明Intent-Filter的組件只能響應指明自己名字的顯式Intent請求,而無法響應隱式Intent請求。
一個聲明了IntentFilter的組件既可以響應顯式Intent請求,也可以響應隱式Intent請求。在通過和 IntentFilter比較來解析隱式Intent請求時,Android將以下三個因素作為選擇的參考標準Action \Data\ Category,而Extra和Flag在解析收到Intent時是並不起作用的
Intent過濾器:
應用程式的組件為了告訴Android自己能響應、處理哪些隱式Intent請求,可以聲明一個甚至多個Intent Filter。每個Intent Filter描述該組件所能響應Intent請求——組件希望接收什麼類型的請求行為,什麼類型的請求資料。
如何為組件聲明自己的Intent Filter? 常見的方法是在AndroidManifest.xml檔案中用屬性<Intent-Filter>描述組件的Intent Filter。
隱式Intent和Intent Filter進行比較時的三要素是Intent的動作、資料以及類別。實際上,一個隱式Intent請求要能夠傳遞給目標組件,必要通過這三個方面的檢查。如果任何一方面不匹配,Android都不會將該隱式Intent傳遞給目標組件。接下來我們講解這三方面檢查的具體規則。
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>的行為測試。
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請求。
Data測試:
<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,它們也需要相匹配才會通過測試
示範一個例子,用Intent隱藏啟動activity,並顯示Action和Category的值,這個例子用到了兩個Activity,兩個布局,要注意到是在AndroidMainFest裡設定uses-permission
首先來看布局檔案main.xml
View Code
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7 <Button android:id="@+id/bt1"
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="intent隱藏啟動test"
11 />
12 </LinearLayout>
second.xml檔案
View Code
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 android:orientation="vertical"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent">
7 <EditText android:id = "@+id/et1"
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"/>
10 <EditText android:id ="@+id/et2"
11 android:layout_width="fill_parent"
12 android:layout_height="wrap_content"/>
13 </LinearLayout>
然後給大家看一下AndroidMainFest裡的代碼
View Code
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="cn.shaoyangjiang.com"
4 android:versionCode="1"
5 android:versionName="1.0">
6 <uses-sdk android:minSdkVersion="8" />
7
8 <application android:icon="@drawable/icon" android:label="@string/app_name">
9 <activity android:name=".IntentFilterActivity"
10 android:label="@string/app_name">
11 <intent-filter>
12 <action android:name="android.intent.action.MAIN" />
13 <category android:name="android.intent.category.LAUNCHER" />
14 </intent-filter>
15 </activity>
16
17 <activity android:name=".Second"
18 android:label="@string/app_name">
19 <intent-filter>
20 <!-- 指定該Activity能響應action為指定字串的Intent -->
21 <action android:name="cn.shaoyangjiang.cn.com" />
22 <!-- 指定該Activity能響應category為指定字串的Intent -->
23 <category android:name="cn.shaoyangjiang.cn" />
24 <!-- 指定該Activity能響應category為android.intent.category.DEFAULT的Intent -->
25 <category android:name="android.intent.category.DEFAULT" />
26 </intent-filter>
27 </activity>
28 </application>
29 <uses-permission android:name="android.permission.READ_CONTACTS" />
30 <uses-permission android:name="android.permission.WRITE_CONTACTS" />
31 </manifest>
第三步:修改intentfilterActivity.java檔案
View Code
1 package cn.shaoyangjiang.com;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.view.View;
7 import android.view.View.OnClickListener;
8 import android.widget.Button;
9
10 public class IntentFilterActivity extends Activity {
11 //定義一個Action常量
12 final static String shao = "cn.shaoyangjiang.cn.com";
13 //定義一個Category常量
14 final static String yang = "cn.shaoyangjiang.cn";
15 @Override
16 public void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.main);
19 Button button1 = (Button)findViewById(R.id.bt1);
20 button1.setOnClickListener(new OnClickListener() {
21 @Override
22 public void onClick(View arg0) {
23 Intent intent = new Intent();
24 //設定Action屬性
25 intent.setAction(shao);
26 //添加Category屬性
27 intent.addCategory(yang);
28 startActivity(intent);
29 }
30 });
31 }
32 }
最後一部是second.java檔案
View Code
1 package cn.shaoyangjiang.com;
2
3 import java.util.Set;
4 import android.app.Activity;
5 import android.os.Bundle;
6 import android.widget.EditText;
7
8 public class Second extends Activity{
9 @Override
10 protected void onCreate(Bundle savedInstanceState) {
11 super.onCreate(savedInstanceState);
12 setContentView(R.layout.second);
13 EditText editText1 =(EditText)findViewById(R.id.et1);
14 //擷取該Activity對應的Intent的Action屬性
15 String actionValue = getIntent().getAction();
16 //顯示Action屬性
17 editText1.setText("Action: "+actionValue);
18 EditText editText2 = (EditText)findViewById(R.id.et2);
19 //擷取該Activity對應的Intent的Category屬性
20 Set<String> cate= getIntent().getCategories();
21 //顯示Action屬性
22 editText2.setText("Categoties: "+cate);
23 }
24 }
: