2011年5月17日 星期二

Implicit Intent

Implicit intent為未指定切換目標的intent
當送出intent後, 系統會根據各個應用程式的intent filter去判斷哪個應用程式要接收
若有多個適合的目標則會讓使用者去選擇

一個intent有下列常用的屬性
1. action : 動作
2. category : 詳細的比對資訊
3. component : 指定特定的目標接受intent
4. data : URI
5. extra : 資料

下例就是建立一個intent
其action是"android.intent.action.MAIN"
而category是"android.intent.category.HOME"
category可以不設定, 也可以設定多個, 這就是為什麼是用addCategory()而不是用setCategory()!
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.HOME")

屬性設定完後送出, 透過intent filter的比對可找出是應用程式接收此intent
startActivity(intent);

在每個應用程式的AndroidManifest.xml
可看到intent filter的設定
下例就是一個intent filter
其action是"android.intent.action.MAIN"
而category是"android.intent.category.LAUNCHER"
action和category都可以設定多個


其中比對action時只要和filter其中一個action相同就算通過action的比對
而比對category時則是要和filter每個category都比對過才算通過category的比對

沒有留言:

張貼留言