基礎總結篇之九:Intent應用詳解

今天咱們來說一下Android中Intent的原理和應用。java

前面咱們總結了幾個Android中重要組件,相信你們對於這些組件已經有了清晰的認識,咱們就來看一下幾個常見的操做:
android

啓動一個Activity:Context.startActivity(Intent intent);
瀏覽器

啓動一個Service:Context.startService(Intent service);
網絡

綁定一個Service:Context.bindService(Intent service, ServiceConnection conn, int flags);
搜索引擎

發送一個Broadcast:Context.sendBroadcast(Intent intent);
google

咱們發現,在這些操做中,都有一個Intent參與其中,看起來像是一個很是重要的組件,那麼Intent究竟是什麼呢?
url

簡單來講,Intent是系統各組件之間進行數據傳遞的數據負載者。當咱們須要作一個調用動做,咱們就能夠經過Intent告訴Android系統來完成這個過程,Intent就是調用通知的一種操做。spa

Intent有幾個重要的屬性,下面咱們將會逐一介紹:code

1.action,要執行的動做component

對於有以下聲明的Activity:

<activity android:name=".TargetActivity">  
    <intent-filter>  
        <action android:name="com.scott.intent.action.TARGET"/>  
        <category android:name="android.intent.category.DEFAULT"/>  
    </intent-filter>  
</activity>

TargetActivity在其<intent-filter>中聲明瞭<action>,即目標action,若是咱們須要作一個跳轉的動做,就須要在Intent中指定目標的action,以下:

public void gotoTargetActivity(View view) {  
    Intent intent = new Intent("com.scott.intent.action.TARGET");  
    startActivity(intent);  
}

當咱們爲Intent指定相應的action,而後調用startActivity方法後,系統會根據action跳轉到對應的Activity。

除了自定義的action以外,Intent也內含了不少默認的action,隨便列舉幾個:

public static final String ACTION_MAIN = "android.intent.action.MAIN";  
public static final String ACTION_VIEW = "android.intent.action.VIEW";  
public static final String ACTION_WEB_SEARCH = "android.intent.action.WEB_SEARCH";  
public static final String ACTION_CALL = "android.intent.action.CALL";

每個action都有其特定的用途,下文也會使用到它們。

2.data和extras,即執行動做要操做的數據和傳遞到目標的附加信息

下面就舉一個與瀏覽器交互的例子:

/** 
 * 打開指定網頁 
 * @param view 
 */  
public void invokeWebBrowser(View view) {  
    Intent intent = new Intent(Intent.ACTION_VIEW);  
    intent.setData(Uri.parse("http://www.google.com.hk"));  
    startActivity(intent);  
}  
  
/** 
 * 進行關鍵字搜索 
 * @param view 
 */  
public void invokeWebSearch(View view) {  
    Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);  
    intent.putExtra(SearchManager.QUERY, "android");    //關鍵字  
    startActivity(intent);  
}

上面兩個方法分別是啓動瀏覽器並打開指定網頁、進行關鍵字搜索,分別對應的action是Intent.ACTION_VIEW和Intent.ACTION_WEB_SEARCH,前者需指定相應的網頁地址,後者需指定關鍵字信息,對於關鍵字搜索來講,瀏覽器會按照本身設置的默認的搜索引擎進行搜索。

咱們注意到,在打開網頁時,爲Intent指定一個data屬性,這實際上是指定要操做的數據,是一個URI的形式,咱們能夠將一個指定前綴的字符串轉換成特定的URI類型,如:「http:」或「https:」表示網絡地址類型,「tel:」表示電話號碼類型,「mailto:」表示郵件地址類型,等等。例如,咱們要呼叫給定的號碼,能夠這樣作:

public void call(View view) {  
    Intent intent = new Intent(Intent.ACTION_CALL);  
    intent.setData(Uri.parse("tel:12345678"));  
    startActivity(intent);  
}

那麼咱們如何知道目標是否接受這種前綴呢?這就須要看一下目標中<data/>元素的匹配規則了。

在目標<data/>標籤中包含了如下幾種子元素,他們定義了url的匹配規則:

android:scheme 匹配url中的前綴,除了「http」、「https」、「tel」...以外,咱們能夠定義本身的前綴

android:host 匹配url中的主機名部分,如「google.com」,若是定義爲「*」則表示任意主機名

android:port 匹配url中的端口

android:path 匹配url中的路徑

咱們改動一下TargetActivity的聲明信息:

<activity android:name=".TargetActivity">  
    <intent-filter>  
        <action android:name="com.scott.intent.action.TARGET"/>  
        <category android:name="android.intent.category.DEFAULT"/>  
        <data android:scheme="scott" android:host="com.scott.intent.data" android:port="7788" android:path="/target"/>  
    </intent-filter>  
</activity>

這個時候若是隻指定action就不夠了,咱們須要爲其設置data值,以下:

public void gotoTargetActivity(View view) {  
    Intent intent = new Intent("com.scott.intent.action.TARGET");  
    intent.setData(Uri.parse("scott://com.scott.intent.data:7788/target"));  
    startActivity(intent);  
}

此時,url中的每一個部分和TargetActivity配置信息中所有一致才能跳轉成功,不然就被系統拒絕。

不過有時候對path限定死了也不太好,好比咱們有這樣的url:(scott://com.scott.intent.data:7788/target/hello)(scott://com.scott.intent.data:7788/target/hi

這個時候該怎麼辦呢?咱們須要使用另一個元素:android:pathPrefix,表示路徑前綴。

咱們把android:path="/target"修改成android:pathPrefix="/target",而後就能夠知足以上的要求了。

而在進行搜索時,咱們使用了一個putExtra方法,將關鍵字作爲參數放置在Intent中,咱們成爲extras(附加信息),這裏面涉及到了一個Bundle對象。

Bundle和Intent有着密不可分的關係,主要負責爲Intent保存附加參數信息,它實現了android.os.Paracelable接口,內部維護一個Map類型的屬性,用於以鍵值對的形式存放附加參數信息。在咱們使用Intent的putExtra方法放置附加信息時,該方法會檢查默認的Bundle實例爲不爲空,若是爲空,則新建立一個Bundle實例,而後將具體的參數信息放置到Bundle實例中。咱們也能夠本身建立Bundle對象,而後爲Intent指定這個Bundle便可,以下:

public void gotoTargetActivity(View view) {  
    Intent intent = new Intent("com.scott.intent.action.TARGET");  
    Bundle bundle = new Bundle();  
    bundle.putInt("id", 0);  
    bundle.putString("name", "scott");  
    intent.putExtras(bundle);  
    startActivity(intent);  
}

須要注意的是,在使用putExtras方法設置Bundle對象以後,系統進行的不是引用操做,而是複製操做,因此若是設置完以後再更改bundle實例中的數據,將不會影響Intent內部的附加信息。那咱們如何獲取設置在Intent中的附加信息呢?與之對應的是,咱們要從Intent中獲取到Bundle實例,而後再從中取出對應的鍵值信息:

Bundle bundle = intent.getExtras();  
int id = bundle.getInt("id");  
String name = bundle.getString("name");

固然咱們也可使用Intent的getIntExtra和getStringExtra方法獲取,其數據源都是Intent中的Bundle類型的實例對象。

前面咱們涉及到了Intent的三個屬性:action、data和extras。除此以外,Intent還包括如下屬性:

3.category,要執行動做的目標所具備的特質或行爲歸類

例如:在咱們的應用主界面Activity一般有以下配置:

<category android:name="android.intent.category.LAUNCHER" />

表明該目標Activity是該應用所在task中的初始Activity而且出如今系統launcher的應用列表中。

幾個常見的category以下:

Intent.CATEGORY_DEFAULT(android.intent.category.DEFAULT) 默認的category

Intent.CATEGORY_PREFERENCE(android.intent.category.PREFERENCE) 表示該目標Activity是一個首選項界面;

Intent.CATEGORY_BROWSABLE(android.intent.category.BROWSABLE)指定了此category後,在網頁上點擊圖片或連接時,系統會考慮將此目標Activity列入可選列表,供用戶選擇以打開圖片或連接。

在爲Intent設置category時,應使用addCategory(String category)方法向Intent中添加指定的類別信息,來匹配聲明瞭此類別的目標Activity。

4.type:要執行動做的目標Activity所能處理的MIME數據類型

例如:一個能夠處理圖片的目標Activity在其聲明中包含這樣的mimeType:

<data android:mimeType="image/*" />

在使用Intent進行匹配時,咱們可使用setType(String type)或者setDataAndType(Uri data, String type)來設置mimeType。

5.component,目標組件的包或類名稱

在使用component進行匹配時,通常採用如下幾種形式:

intent.setComponent(new ComponentName(getApplicationContext(), TargetActivity.class));  
intent.setComponent(new ComponentName(getApplicationContext(), "com.scott.intent.TargetActivity"));  
intent.setComponent(new ComponentName("com.scott.other", "com.scott.other.TargetActivity"));

其中,前兩種是用於匹配同一包內的目標,第三種是用於匹配其餘包內的目標。須要注意的是,若是咱們在Intent中指定了component屬性,系統將不會再對action、data/type、category進行匹配。

今天就先講到這裏吧,有時間的話再加以補充。

相關文章
相關標籤/搜索