Android中Intent組件詳解

Intent是不一樣組件之間相互通信的紐帶,封裝了不一樣組件之間通信的條件。Intent 自己是定義爲一個類別(Class),一個Intent對象表達一個目的(Goal)或指望(Expectation),敘述其所指望的服務或動做、與動 做有關的數據等。Android則根據此Intent對象之敘述,負責配對,找出相配的組件,而後將 Intent對象傳遞給所找到的組件,Android的媒婆任務就完成了。 java

 

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

對於每種組件來講,激活的方法是不一樣的:
1.經過傳遞一個 Intent對象至 Context.startActivity()或Activity.startActivityForResult()以載入(或指定新工做給)一個 activity。相應的activity能夠經過調用 getIntent() 方法來查看激活它的intent。Android經過調用activity的onNewIntent()方法來傳遞給它繼發的intent。
web

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

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

與此相似,一個Intent能夠被調用組件傳遞給 Context.bindService()以獲取一個正在運行的目標服務的鏈接。這個服務會經由onBind() 方法的調用獲取這個Intent對象(若是服務還沒有啓動,bindService()會先啓動它)。好比說,一個activity能夠鏈接至前述的音樂回 放服務,並提供給用戶一個可操做的(用戶界面)以對回放進行控制。這個activity能夠調用 bindService() 來創建鏈接,而後調用服務中定義的對象來影響回放。 異步

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

 

Intent對象包含的內容 this

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

// 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類型的對象。
用法:
.net


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);




注意:有的須要配置必定的權限

相關文章
相關標籤/搜索