Android中Intent組件詳解

jopen 10年前發布 | 27K 次閱讀 Android Android開發 移動開發

Intent是不同組件之間相互通訊的紐帶,封裝了不同組件之間通訊的條件。Intent 本身是定義為一個類別(Class),一個Intent對象表達一個目的(Goal)或期望(Expectation),敘述其所期望的服務或動作、與動 作有關的數據等。Android則根據此Intent對象之敘述,負責配對,找出相配的組件,然后將 Intent對象傳遞給所找到的組件,Android的媒婆任務就完成了。

 

在Google Doc中是這樣描述Intent的(摘自Android中文翻譯組)
當接收到ContentResolver發出的請求后,內容提供者被激活。而其它三種組件──activity、服務和廣播接收器被一種叫做intent的異步消息所激活。intent 是一個保存著消息內容的Intent對 象。對于activity和服務來說,它指明了請求的操作名稱以及作為操作對象的數據的URI和其它一些信息。比如說,它可以承載對一個activity 的請求,讓它為用戶顯示一張圖片,或者讓用戶編輯一些文本。而對于廣播接收器而言,Intent對象指明了聲明的行為。比如,它可以對所有感興趣的對象聲 明照相按鈕被按下。

對于每種組件來說,激活的方法是不同的:
1.通過傳遞一個 Intent對象至 Context.startActivity()或Activity.startActivityForResult()以載入(或指定新工作給)一個 activity。相應的activity可以通過調用 getIntent() 方法來查看激活它的intent。Android通過調用activity的onNewIntent()方法來傳遞給它繼發的intent。

一個activity經常啟動了下一個。如果它期望它所啟動的那個 activity返回一個結果,它會以調用startActivityForResult()來取代startActivity()。比如說,如果它啟動 了另外一個activity以使用戶挑選一張照片,它也許想知道哪張照片被選中了。結果將會被封裝在一個Intent對象中,并傳遞給發出調用的 activity的onActivityResult() 方法。

2.通過傳遞一個Intent對象至Context.startService()將啟動一個服務(或給予正在運行的服務以一個新的指令)。Android調用服務的onStart()方法并將Intent對象傳遞給它。

與此類似,一個Intent可以被調用組件傳遞給 Context.bindService()以獲取一個正在運行的目標服務的連接。這個服務會經由onBind() 方法的調用獲取這個Intent對象(如果服務尚未啟動,bindService()會先啟動它)。比如說,一個activity可以連接至前述的音樂回 放服務,并提供給用戶一個可操作的(用戶界面)以對回放進行控制。這個activity可以調用 bindService() 來建立連接,然后調用服務中定義的對象來影響回放。

3.應用程序可以憑借將Intent對象傳遞給 Context.sendBroadcast() ,Context.sendOrderedBroadcast(), 以及Context.sendStickyBroadcast()和其它類似方法來產生一個廣播。Android會調用所有對此廣播有興趣的廣播接收器的 onReceive()方法將intent傳遞給它們。

 

Intent對象包含的內容

在Intent類的Java源代碼中定義了Intent相關內容的變量,如下:

// Action  
private String mAction;  
// Data  
private Uri mData;  
private String mType;  
private String mPackage;  
// ComponentName  
private ComponentName mComponent;  
// Flag  
private int mFlags;  
// category  
private HashSet<String> mCategories;  
// extras  
private Bundle mExtras;

1.componentName(組件名稱),指定Intent的目標組件的類名稱。組件名稱是可選的,如果填寫,Intent對象會發送給指定組件名稱的組件,否則也可以通過其他Intent信息定位到適合的組件。組件名稱是個ComponentName類型的對象。
用法:


Intent intent = new Intent();  
    // 構造的參數為當前Context和目標組件的類路徑名  
    ComponentName cn = new ComponentName(HelloActivity.this, "com.byread.activity.OtherActivity");  
    intent.setComponent(cn);  
    startActivity(intent);




相當于以下常用方法:
Intent intent = new Intent();  
    intent.setClass(HelloActivity.this, OtherActivity.class);  
    startActivity(intent);




Intent類中也包含一個初始化ComponentName的構造函數: 

public Intent(Context packageContext, Class<?> cls) {  
        mComponent = new ComponentName(packageContext, cls);  
    }




 

2.action(動作),指定Intent的執行動作,比如調用撥打電話組件。 

public Intent(String action) {  
        mAction = action;  
    }



3.data(數據),起到表示數據和數據MIME類型的作用。不同的action是和不同的data類型配套的,通過設置data的Uri來獲得。 

public Intent(String action, Uri uri) {  
    mAction = action;  
    mData = uri;  
}




比如調用撥打電話組件:

Uri uri = Uri.parse("tel:10086");  
// 參數分別為調用撥打電話組件的Action和獲取Data數據的Uri  
Intent intent = new Intent(Intent.ACTION_DIAL, uri);  
startActivity(intent);




4.category(類別),被執行動作的附加信息。例如應用的啟動Activity在intent-filter中設置category。

<intent-filter>  
        <action android:name="android.intent.action.MAIN" />  
        <category android:name="android.intent.category.LAUNCHER" />  
    </intent-filter>




 

5.extras(附加信息),為處理Intent組件提供附加的信息。可通過putXX()和getXX()方法存取信息;也可以通過創建Bundle對象,再通過putExtras()和getExtras()方法來存取。 

 

6.flags(標記),指示Android如何啟動目標Activity,設置方法為調用Intent的setFlags方法。常用的Flags參數有:

FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_NO_HISTORY
FLAG_ACTIVITY_SINGLE_TOP  

 

Intent的投遞

1.顯式方式。直接設置目標組件的ComponentName,用于一個應用內部的消息傳遞,比如啟動另一個Activity或者一個services。
通過Intent的setComponent和setClass來制定目標組件的ComponentName。

 

2.隱式方式。ComponentName為空,用于調用其他應用中的組件。需要包含足夠的信息,這樣系統才能根據這些信息使用intent filter在所有的組件中過濾action、data或者category來匹配目標組件。可參考Android中Activity組件詳解(5.Activity的Intent Filter)
如果Intent指明定了action,則目標組件的IntentFilter的action列表中就必須包含有這個action,否則不能匹配;
如果Intent沒有提供type,系統將從data中得到數據類型。和action一樣,目標組件的數據類型列表中必須包含Intent的數據類型,否則不能匹配;
如 果Intent中的數據不是content: 類型的URI,而且Intent也沒有明確指定它的type,將根據Intent中數據的scheme (比如 http: 或者mailto: ) 進行匹配。同上,Intent 的scheme必須出現在目標組件的scheme列表中;
如果Intent指定了一個或多個category,這些類別必須全部出現在組建的類別列表中。比如 Intent中包含了兩個類別:LAUNCHER_CATEGORY 和 ALTERNATIVE_CATEGORY,解析得到的目標組件必須至少包含這兩個類別。

 

Intent調用常見系統組件 

// 調用瀏覽器  
Uri webViewUri = Uri.parse("http://blog.csdn.net/zuolongsnail");  
Intent intent = new Intent(Intent.ACTION_VIEW, webViewUri);  

// 調用地圖  
Uri mapUri = Uri.parse("geo:100,100");  
Intent intent = new Intent(Intent.ACTION_VIEW, mapUri);  

// 播放mp3  
Uri playUri = Uri.parse("file:///sdcard/test.mp3");  
Intent intent = new Intent(Intent.ACTION_VIEW, playUri);  
intent.setDataAndType(playUri, "audio/mp3");  

// 調用撥打電話  
Uri dialUri = Uri.parse("tel:10086");  
Intent intent = new Intent(Intent.ACTION_DIAL, dialUri);  
// 直接撥打電話,需要加上權限<uses-permission id="android.permission.CALL_PHONE" />  
Uri callUri = Uri.parse("tel:10086");  
Intent intent = new Intent(Intent.ACTION_CALL, callUri);  

// 調用發郵件(這里要事先配置好的系統Email,否則是調不出發郵件界面的)  
Uri emailUri = Uri.parse("mailto:zuolongsnail@163.com");  
Intent intent = new Intent(Intent.ACTION_SENDTO, emailUri);  
// 直接發郵件  
Intent intent = new Intent(Intent.ACTION_SEND);  
String[] tos = { "zuolongsnail@gmail.com" };  
String[] ccs = { "zuolongsnail@163.com" };  
intent.putExtra(Intent.EXTRA_EMAIL, tos);  
intent.putExtra(Intent.EXTRA_CC, ccs);  
intent.putExtra(Intent.EXTRA_TEXT, "the email text");  
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");  
intent.setType("text/plain");  
Intent.createChooser(intent, "Choose Email Client");  

// 發短信  
Intent intent = new Intent(Intent.ACTION_VIEW);  
intent.putExtra("sms_body", "the sms text");  
intent.setType("vnd.android-dir/mms-sms");  
// 直接發短信  
Uri smsToUri = Uri.parse("smsto:10086");  
Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);  
intent.putExtra("sms_body", "the sms text");  
// 發彩信  
Uri mmsUri = Uri.parse("content://media/external/images/media/23");  
Intent intent = new Intent(Intent.ACTION_SEND);  
intent.putExtra("sms_body", "the sms text");  
intent.putExtra(Intent.EXTRA_STREAM, mmsUri);  
intent.setType("image/png");  

// 卸載應用  
Uri uninstallUri = Uri.fromParts("package", "com.app.test", null);  
Intent intent = new Intent(Intent.ACTION_DELETE, uninstallUri);  
// 安裝應用  
Intent intent = new Intent(Intent.ACTION_VIEW);  
intent.setDataAndType(Uri.fromFile(new File("/sdcard/test.apk"), "application/vnd.android.package-archive");  

// 在Android Market中查找應用  
Uri uri = Uri.parse("market://search?q=憤怒的小鳥");           
Intent intent = new Intent(Intent.ACTION_VIEW, uri);



原文地址:http://blog.csdn.net/zuolongsnail/article/details/6574211

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!