歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android應用開發中Intent的作用及使用方法

Android應用開發中Intent的作用及使用方法

日期:2017/3/1 9:05:38   编辑:Linux編程

Intent是一種運行時綁定(run-time binding)機制,它能在程序運行過程中連接兩個不同的組件。通過Intent,你的程序可以向Android表達某種請求或者意願,Android會根據意願的內容選擇適當的組件來完成請求。比如,有一個Activity希望打開網頁浏覽器查看某一網頁的內容,那麼這個Activity只需要發出WEB_SEARCH_ACTION給Android,Android就會根據Intent的請求內容,查詢各組件注冊時聲明的IntentFilter,找到網頁浏覽器的Activity來浏覽網頁。

Android的三個基本組件——Activity,Service和Broadcast Receiver——都是通過Intent機制激活的,不同類型的組件有不同的傳遞Intent方式:

要激活一個新的Activity,或者讓一個現有的Activity做新的操作,可以通過調用Context.startActivity()或者Activity.startActivityForResult()方法。

要啟動一個新的Service,或者向一個已有的Service傳遞新的指令,調用Context.startService()方法或者調用Context.bindService()方法將調用此方法的上下文對象與Service綁定。

Context.sendBroadcast()、Context.sendOrderBroadcast()、Context.sendStickBroadcast()這三個方法可以發送Broadcast Intent。發送之後,所有已注冊的並且擁有與之相匹配IntentFilter的BroadcastReceiver就會被激活。

Intent一旦發出,Android都會准確找到相匹配的一個或多個Activity,Service或者BroadcastReceiver作響應。所以,不同類型的Intent消息不會出現重疊,即Broadcast的Intent消息只會發送給BroadcastReceiver,而決不會發送給Activity或者Service。由startActivity()傳遞的消息也只會發給Activity,由startService()傳遞的Intent只會發送給Service。

Intent的構成

要在不同的activity之間傳遞數據,就要在intent中包含相應的內容,一般來說數據中最基本的應該包括:

Action:用來指明要實施的動作是什麼,比如說ACTION_VIEW, ACTION_EDIT等。具體的可以查閱android SDK-> reference中的Android.content.intent類,裡面的constants中定義了所有的action。

一些常用的Action:

1.ACTION_CALL activity 啟動一個電話.

2.ACTION_EDIT activity 顯示用戶編輯的數據.

3.ACTION_MAIN activity 作為Task中第一個Activity啟動

4.ACTION_SYNC activity 同步手機與數據服務器上的數據.

5.ACTION_BATTERY_LOW broadcast receiver 電池電量過低警告.

6.ACTION_HEADSET_PLUG broadcast receiver 插拔耳機警告

7.ACTION_SCREEN_ON broadcast receiver 屏幕變亮警告.

8.ACTION_TIMEZONE_CHANGED broadcast receiver 改變時區警告.

Activity組件的激活

對於每種組件來說,激活的方法是不同的:

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://www.linuxidc.com/linuxidc");
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:[email protected]");
Intent intent = new Intent(Intent.ACTION_SENDTO, emailUri);
// 直接發郵件
Intent intent = new Intent(Intent.ACTION_SEND);
String[] tos = { "[email protected]" };
String[] ccs = { "[email protected]" };
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);

注意:有的需要配置一定的權限。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved