詳解Android應(yīng)用開發(fā)中Intent的作用及使用方法
Intent是一種運(yùn)行時(shí)綁定(run-time binding)機(jī)制,它能在程序運(yùn)行過程中連接兩個(gè)不同的組件。通過Intent,你的程序可以向Android表達(dá)某種請求或者意愿,Android會(huì)根據(jù)意愿的內(nèi)容選擇適當(dāng)?shù)慕M件來完成請求。比如,有一個(gè)Activity希望打開網(wǎng)頁瀏覽器查看某一網(wǎng)頁的內(nèi)容,那么這個(gè)Activity只需要發(fā)出WEB_SEARCH_ACTION給Android,Android就會(huì)根據(jù)Intent的請求內(nèi)容,查詢各組件注冊時(shí)聲明的IntentFilter,找到網(wǎng)頁瀏覽器的Activity來瀏覽網(wǎng)頁。
Android的三個(gè)基本組件——Activity,Service和Broadcast Receiver——都是通過Intent機(jī)制激活的,不同類型的組件有不同的傳遞Intent方式:
要激活一個(gè)新的Activity,或者讓一個(gè)現(xiàn)有的Activity做新的操作,可以通過調(diào)用Context.startActivity()或者Activity.startActivityForResult()方法。
要啟動(dòng)一個(gè)新的Service,或者向一個(gè)已有的Service傳遞新的指令,調(diào)用Context.startService()方法或者調(diào)用Context.bindService()方法將調(diào)用此方法的上下文對象與Service綁定。
Context.sendBroadcast()、Context.sendOrderBroadcast()、Context.sendStickBroadcast()這三個(gè)方法可以發(fā)送Broadcast Intent。發(fā)送之后,所有已注冊的并且擁有與之相匹配IntentFilter的BroadcastReceiver就會(huì)被激活。
Intent一旦發(fā)出,Android都會(huì)準(zhǔn)確找到相匹配的一個(gè)或多個(gè)Activity,Service或者BroadcastReceiver作響應(yīng)。所以,不同類型的Intent消息不會(huì)出現(xiàn)重疊,即Broadcast的Intent消息只會(huì)發(fā)送給BroadcastReceiver,而決不會(huì)發(fā)送給Activity或者Service。由startActivity()傳遞的消息也只會(huì)發(fā)給Activity,由startService()傳遞的Intent只會(huì)發(fā)送給Service。
Intent的構(gòu)成
要在不同的activity之間傳遞數(shù)據(jù),就要在intent中包含相應(yīng)的內(nèi)容,一般來說數(shù)據(jù)中最基本的應(yīng)該包括:
Action:用來指明要實(shí)施的動(dòng)作是什么,比如說ACTION_VIEW, ACTION_EDIT等。具體的可以查閱android SDK-> reference中的Android.content.intent類,里面的constants中定義了所有的action。
一些常用的Action:
- ACTION_CALL activity 啟動(dòng)一個(gè)電話.
- ACTION_EDIT activity 顯示用戶編輯的數(shù)據(jù).
- ACTION_MAIN activity 作為Task中第一個(gè)Activity啟動(dòng)
- ACTION_SYNC activity 同步手機(jī)與數(shù)據(jù)服務(wù)器上的數(shù)據(jù).
- ACTION_BATTERY_LOW broadcast receiver 電池電量過低警告.
- ACTION_HEADSET_PLUG broadcast receiver 插拔耳機(jī)警告
- ACTION_SCREEN_ON broadcast receiver 屏幕變亮警告.
- ACTION_TIMEZONE_CHANGED broadcast receiver 改變時(shí)區(qū)警告.
Activity組件的激活
對于每種組件來說,激活的方法是不同的:
1.通過傳遞一個(gè)Intent對象至 Context.startActivity()或Activity.startActivityForResult()以載入(或指定新工作給)一個(gè)activity。相應(yīng)的activity可以通過調(diào)用 getIntent() 方法來查看激活它的intent。Android通過調(diào)用activity的onNewIntent()方法來傳遞給它繼發(fā)的intent。
一個(gè)activity經(jīng)常啟動(dòng)了下一個(gè)。如果它期望它所啟動(dòng)的那個(gè)activity返回一個(gè)結(jié)果,它會(huì)以調(diào)用startActivityForResult()來取代startActivity()。比如說,如果它啟動(dòng)了另外一個(gè)activity以使用戶挑選一張照片,它也許想知道哪張照片被選中了。結(jié)果將會(huì)被封裝在一個(gè)Intent對象中,并傳遞給發(fā)出調(diào)用的activity的onActivityResult() 方法。
2.通過傳遞一個(gè)Intent對象至Context.startService()將啟動(dòng)一個(gè)服務(wù)(或給予正在運(yùn)行的服務(wù)以一個(gè)新的指令)。Android調(diào)用服務(wù)的onStart()方法并將Intent對象傳遞給它。
與此類似,一個(gè)Intent可以被調(diào)用組件傳遞給 Context.bindService()以獲取一個(gè)正在運(yùn)行的目標(biāo)服務(wù)的連接。這個(gè)服務(wù)會(huì)經(jīng)由onBind() 方法的調(diào)用獲取這個(gè)Intent對象(如果服務(wù)尚未啟動(dòng),bindService()會(huì)先啟動(dòng)它)。比如說,一個(gè)activity可以連接至前述的音樂回放服務(wù),并提供給用戶一個(gè)可操作的(用戶界面)以對回放進(jìn)行控制。這個(gè)activity可以調(diào)用 bindService() 來建立連接,然后調(diào)用服務(wù)中定義的對象來影響回放。
3.應(yīng)用程序可以憑借將Intent對象傳遞給 Context.sendBroadcast() ,Context.sendOrderedBroadcast(), 以及Context.sendStickyBroadcast()和其它類似方法來產(chǎn)生一個(gè)廣播。Android會(huì)調(diào)用所有對此廣播有興趣的廣播接收器的 onReceive()方法將intent傳遞給它們。
Intent對象包含的內(nèi)容
在Intent類的Java源代碼中定義了Intent相關(guān)內(nèi)容的變量,如下:
// 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的目標(biāo)組件的類名稱。組件名稱是可選的,如果填寫,Intent對象會(huì)發(fā)送給指定組件名稱的組件,否則也可以通過其他Intent信息定位到適合的組件。組件名稱是個(gè)ComponentName類型的對象。
用法:
Intent intent = new Intent(); // 構(gòu)造的參數(shù)為當(dāng)前Context和目標(biāo)組件的類路徑名 ComponentName cn = new ComponentName(HelloActivity.this, "com.byread.activity.OtherActivity"); intent.setComponent(cn); startActivity(intent);
相當(dāng)于以下常用方法:
Intent intent = new Intent(); intent.setClass(HelloActivity.this, OtherActivity.class); startActivity(intent); Intent類中也包含一個(gè)初始化ComponentName的構(gòu)造函數(shù): public Intent(Context packageContext, Class<?> cls) { mComponent = new ComponentName(packageContext, cls); }
2.action(動(dòng)作),指定Intent的執(zhí)行動(dòng)作,比如調(diào)用撥打電話組件。
public Intent(String action) { mAction = action; }
3.data(數(shù)據(jù)),起到表示數(shù)據(jù)和數(shù)據(jù)MIME類型的作用。不同的action是和不同的data類型配套的,通過設(shè)置data的Uri來獲得。
public Intent(String action, Uri uri) { mAction = action; mData = uri; }
比如調(diào)用撥打電話組件:
Uri uri = Uri.parse("tel:10086"); // 參數(shù)分別為調(diào)用撥打電話組件的Action和獲取Data數(shù)據(jù)的Uri Intent intent = new Intent(Intent.ACTION_DIAL, uri); startActivity(intent);
4.category(類別),被執(zhí)行動(dòng)作的附加信息。例如應(yīng)用的啟動(dòng)Activity在intent-filter中設(shè)置category。
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
5.extras(附加信息),為處理Intent組件提供附加的信息??赏ㄟ^putXX()和getXX()方法存取信息;也可以通過創(chuàng)建Bundle對象,再通過putExtras()和getExtras()方法來存取。
6.flags(標(biāo)記),指示Android如何啟動(dòng)目標(biāo)Activity,設(shè)置方法為調(diào)用Intent的setFlags方法。常用的Flags參數(shù)有:
FLAG_ACTIVITY_CLEAR_TOP FLAG_ACTIVITY_NEW_TASK FLAG_ACTIVITY_NO_HISTORY FLAG_ACTIVITY_SINGLE_TOP
Intent的投遞
1.顯式方式。直接設(shè)置目標(biāo)組件的ComponentName,用于一個(gè)應(yīng)用內(nèi)部的消息傳遞,比如啟動(dòng)另一個(gè)Activity或者一個(gè)services。
通過Intent的setComponent和setClass來制定目標(biāo)組件的ComponentName。
2.隱式方式。ComponentName為空,用于調(diào)用其他應(yīng)用中的組件。需要包含足夠的信息,這樣系統(tǒng)才能根據(jù)這些信息使用intent filter在所有的組件中過濾action、data或者category來匹配目標(biāo)組件??蓞⒖糀ndroid中Activity組件詳解(5.Activity的Intent Filter)
如果Intent指明定了action,則目標(biāo)組件的IntentFilter的action列表中就必須包含有這個(gè)action,否則不能匹配;
如果Intent沒有提供type,系統(tǒng)將從data中得到數(shù)據(jù)類型。和action一樣,目標(biāo)組件的數(shù)據(jù)類型列表中必須包含Intent的數(shù)據(jù)類型,否則不能匹配;
如果Intent中的數(shù)據(jù)不是content: 類型的URI,而且Intent也沒有明確指定它的type,將根據(jù)Intent中數(shù)據(jù)的scheme (比如 http: 或者mailto: ) 進(jìn)行匹配。同上,Intent 的scheme必須出現(xiàn)在目標(biāo)組件的scheme列表中;
如果Intent指定了一個(gè)或多個(gè)category,這些類別必須全部出現(xiàn)在組建的類別列表中。比如 Intent中包含了兩個(gè)類別:LAUNCHER_CATEGORY 和 ALTERNATIVE_CATEGORY,解析得到的目標(biāo)組件必須至少包含這兩個(gè)類別。
Intent調(diào)用常見系統(tǒng)組件
// 調(diào)用瀏覽器 Uri webViewUri = Uri.parse("http://blog.csdn.net/zuolongsnail"); Intent intent = new Intent(Intent.ACTION_VIEW, webViewUri); // 調(diào)用地圖 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"); // 調(diào)用撥打電話 Uri dialUri = Uri.parse("tel:10086"); Intent intent = new Intent(Intent.ACTION_DIAL, dialUri); // 直接撥打電話,需要加上權(quán)限<uses-permission id="android.permission.CALL_PHONE" /> Uri callUri = Uri.parse("tel:10086"); Intent intent = new Intent(Intent.ACTION_CALL, callUri); // 調(diào)用發(fā)郵件(這里要事先配置好的系統(tǒng)Email,否則是調(diào)不出發(fā)郵件界面的) Uri emailUri = Uri.parse("mailto:zuolongsnail@163.com"); Intent intent = new Intent(Intent.ACTION_SENDTO, emailUri); // 直接發(fā)郵件 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"); // 發(fā)短信 Intent intent = new Intent(Intent.ACTION_VIEW); intent.putExtra("sms_body", "the sms text"); intent.setType("vnd.android-dir/mms-sms"); // 直接發(fā)短信 Uri smsToUri = Uri.parse("smsto:10086"); Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri); intent.putExtra("sms_body", "the sms text"); // 發(fā)彩信 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"); // 卸載應(yīng)用 Uri uninstallUri = Uri.fromParts("package", "com.app.test", null); Intent intent = new Intent(Intent.ACTION_DELETE, uninstallUri); // 安裝應(yīng)用 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File("/sdcard/test.apk"), "application/vnd.android.package-archive"); // 在Android Market中查找應(yīng)用 Uri uri = Uri.parse("market://search?q=憤怒的小鳥"); Intent intent = new Intent(Intent.ACTION_VIEW, uri);
注意:有的需要配置一定的權(quán)限
- Android實(shí)現(xiàn)打開各種文件的intent方法小結(jié)
- Android編程開發(fā)之打開文件的Intent及使用方法
- Android開發(fā)實(shí)現(xiàn)的Intent跳轉(zhuǎn)工具類實(shí)例
- Android使用Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)
- Android使用Intent獲取聯(lián)系人信息
- Android Intent的幾種用法詳細(xì)解析
- Android Intent啟動(dòng)別的應(yīng)用實(shí)現(xiàn)方法
- 詳解Android中Intent的使用方法
- Android系列之Intent傳遞對象的幾種實(shí)例方法
- Android開發(fā)中使用Intent打開第三方應(yīng)用及驗(yàn)證可用性的方法詳解
相關(guān)文章
Android使用xml文件資源定義菜單實(shí)現(xiàn)方法示例
這篇文章主要介紹了Android使用xml文件資源定義菜單實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android資源文件管理及xml配置自定義菜單相關(guān)操作技巧,需要的朋友可以參考下2019-03-03手把手教你用ViewPager自定義實(shí)現(xiàn)Banner輪播
這篇文章主要手把手教你用ViewPager自定義實(shí)現(xiàn)Banner輪播,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Android應(yīng)用借助LinearLayout實(shí)現(xiàn)垂直水平居中布局
這篇文章主要介紹了Android應(yīng)用借助LinearLayout實(shí)現(xiàn)垂直水平居中布局的方法,文中列舉了LinearLayout線性布局下居中相關(guān)的幾個(gè)重要參數(shù),需要的朋友可以參考下2016-04-04

Android?模擬地圖定位功能的實(shí)現(xiàn)

Android進(jìn)階Handler應(yīng)用線上卡頓監(jiān)控詳解

Android中用Builder模式自定義Dialog的方法

創(chuàng)建子線程對Android進(jìn)行網(wǎng)絡(luò)訪問

Android EditText長按菜單中分享功能的隱藏方法