看似尋常最奇崛,成如容易卻艱辛。北宋.王安石
看似普通的事情其實最不同尋常,並不是簡簡單單就可以做好的;成功看起來似乎很容易,而成功的過程卻充滿着艱辛。
對於我們認爲很普通的事情,不屑一顧,就永遠不會有長進,腳踏實地,就離成功又近一步;成功並不像看到的那麼容易,尋找捷徑是不可取的,我們往往要比別人付出更多的辛勤和努力。
今天我們來講一下Android中Intent的原理和應用。
前面我們總結了幾個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);
我們發現,在這些操作中,都有一個Intent參與其中,看起來像是一個非常重要的組件,那麼Intent到底是什麼呢?
簡單來說,Intent是系統各組件之間進行數據傳遞的數據負載者。當我們需要做一個調用動作,我們就可以通過Intent告訴Android系統來完成這個過程,Intent就是調用通知的一種操作。
Intent有幾個重要的屬性,下面我們將會逐一介紹:
1.action,要執行的動作
對於有如下聲明的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>
- 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);
- }
我們注意到,在打開網頁時,爲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>
- 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);
- }
不過有時候對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);
- }
- 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" />
幾個常見的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/*" />
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進行匹配。
今天就先講到這裏吧,有時間的話再加以補充。