Android中Intent機(jī)制詳解及示例總結(jié)(總結(jié)篇)
最近在進(jìn)行android開發(fā)過程中,在將 Intent傳遞給調(diào)用的組件并完成組件的調(diào)用時遇到點(diǎn)困難,并且之前對Intent的學(xué)習(xí)也是一知半解,最近特意為此拿出一些時間,對Intent部分進(jìn)行了系統(tǒng)的學(xué)習(xí)并進(jìn)行了部分實(shí)踐,下面將自己的學(xué)習(xí)及Intent知識進(jìn)行了詳細(xì)的歸納整理,希望能幫助到同樣遇到相同問題的博友。
下面是Intent介紹、詳解及Intent示例總結(jié):
一.Intent介紹:
Intent的中文意思是“意圖,意向”,在Android中提供了Intent機(jī)制來協(xié)助應(yīng)用間的交互與通訊,Intent負(fù)責(zé)對應(yīng)用中一次操作的動 作、動作涉及數(shù)據(jù)、附加數(shù)據(jù)進(jìn)行描述,Android則根據(jù)此Intent的描述,負(fù)責(zé)找到對應(yīng)的組件,將 Intent傳遞給調(diào)用的組件,并完成組件的調(diào)用。Intent不僅可用于應(yīng)用程序之間,也可用于應(yīng)用程序內(nèi)部的Activity/Service之間的 交互。因此,可以將Intent理解為不同組件之間通信的“媒介”專門提供組件互相調(diào)用的相關(guān)信息。
二、Intent作用:
Intent 是一個將要執(zhí)行的動作的抽象的描述,一般來說是作為參數(shù)來使用,由Intent來協(xié)助完成android各個組件之間的通訊。比如說調(diào)用 startActivity()來啟動一個activity,或者由broadcaseIntent()來傳遞給所有感興趣的 BroadcaseReceiver, 再或者由startService()/bindservice()來啟動一個后臺的service.所以可以看出來,intent主要是用來啟動其他的 activity 或者service,所以可以將intent理解成activity之間的粘合劑。
三.Inten啟動組件的方法:
Intent可以啟動一個Activity,也可以啟動一個Service,還可以發(fā)起一個廣播Broadcasts。具體方法如下:
|
組件名稱 |
方法名稱 |
|
Activity |
startActvity( ) startActivity( ) |
|
Service |
startService( ) bindService( ) |
|
Broadcasts |
sendBroadcasts( ) sendOrderedBroadcasts( ) sendStickyBroadcasts( ) |
四.Intent的幾個重要屬性,下面進(jìn)行詳解:
動作(Action),數(shù)據(jù)(Data),分類(Category),類型(Type),組件(Compent)以及擴(kuò)展信(Extra)。其中最常用的是Action屬性和Data屬性。
1.Action屬性:
對于有如下聲明的Activity:
<activity android:name=".TargetActivity"> <intent-filter> <action android:name="com.scott.intent.action.TARGET"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>
TargetActivity在其<intent-filter>中聲明了<action>,即目標(biāo)action,如果我們需要做一個跳轉(zhuǎn)的動作,就需要在Intent中指定目標(biāo)的action,如下:
public void gotoTargetActivity(View view) {
Intent intent = new Intent("com.scott.intent.action.TARGET");
startActivity(intent);
}
當(dāng)我們?yōu)镮ntent指定相應(yīng)的action,然后調(diào)用startActivity方法后,系統(tǒng)會根據(jù)action跳轉(zhuǎn)到對應(yīng)的Activity。
除了自定義的action之外,Intent也內(nèi)含了很多默認(rèn)的action,下面列舉幾個:
public static final String ACTION_MAIN = "android.intent.action.MAIN"; public static final String ACTION_VIEW = "android.intent.action.VIEW"; public static final String ACTION_WEB_SEARCH = "android.intent.action.WEB_SEARCH"; public static final String ACTION_CALL = "android.intent.action.CALL";
每一個action都有其特定的用途。
2.data和extras,即執(zhí)行動作要操作的數(shù)據(jù)和傳遞到目標(biāo)的附加信息:
下面舉一個與瀏覽器交互的例子:
/**
* 打開指定網(wǎng)頁
* @param view
*/
public void invokeWebBrowser(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com.hk"));
startActivity(intent);
}
/**
* 進(jìn)行關(guān)鍵字搜索
* @param view
*/
public void invokeWebSearch(View view) {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "android"); //關(guān)鍵字
startActivity(intent);
}
上面兩種方法分別是啟動瀏覽器并打開指定網(wǎng)頁、進(jìn)行關(guān)鍵字搜索,分別對應(yīng)的action是Intent.ACTION_VIEW和 Intent.ACTION_WEB_SEARCH,前者需指定相應(yīng)的網(wǎng)頁地址,后者需指定關(guān)鍵字信息,對于關(guān)鍵字搜索來說,瀏覽器會按照自己設(shè)置的默認(rèn) 的搜索引擎進(jìn)行搜索。
我們注意到,在打開網(wǎng)頁時,為Intent指定一個data屬性,這其實(shí)是指定要操作的數(shù)據(jù),是一個URI的形式,我們可以將一個指定前綴的字符串轉(zhuǎn)換成 特定的URI類型,如:“http:”或“https:”表示網(wǎng)絡(luò)地址類型,“tel:”表示電話號碼類型,“mailto:”表示郵件地址類型,等等。
例如,我們要呼叫給定的號碼,可以這樣做:
public void call(View view) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"));
startActivity(intent);
}
那么我們?nèi)绾沃滥繕?biāo)是否接受這種前綴呢?這就需要看一下目標(biāo)中<data/>元素的匹配規(guī)則了。
在目標(biāo)<data/>標(biāo)簽中包含了以下幾種子元素,他們定義了url的匹配規(guī)則:
android:scheme 匹配url中的前綴,除了“http”、“https”、“tel”...之外,我們可以定義自己的前綴
android:host 匹配url中的主機(jī)名部分,如“google.com”,如果定義為“*”則表示任意主機(jī)名
android:port 匹配url中的端口
android:path 匹配url中的路徑
我們改動一下TargetActivity的聲明信息:
<activity android:name=".TargetActivity"> <intent-filter> <action android:name="com.scott.intent.action.TARGET"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="scott" android:host="com.scott.intent.data" android:port="" android:path="/target"/> </intent-filter> </activity>
這個時候如果只指定action就不夠了,我們需要為其設(shè)置data值,如下:
|
常量
|
解釋
|
|
CATEGORY_DEFAULT
|
默認(rèn)的category
|
|
CATEGORY_BROWSABLE
|
指定了此category后,在網(wǎng)頁上點(diǎn)擊圖片或鏈接時,系統(tǒng)會考慮將此目標(biāo)Activity列入可選列表,供用戶選擇以打開圖片或鏈接。
|
|
CATEGORY_GADGET
|
The activity can be embedded inside of another activity that hosts gadgets.
|
|
CATEGORY_HOME
|
The activity displays the home screen, the first screen the user sees when the device is turned on or when the HOME key is pressed.
|
|
CATEGORY_LAUNCHER
|
The activity can be the initial activity of a task and is listed in the top-level application launcher.
|
|
CATEGORY_PREFERENCE
|
表示該目標(biāo)Activity是一個首選項(xiàng)界面;
|
public void gotoTargetActivity(View view) {
Intent intent = new Intent("com.scott.intent.action.TARGET");
intent.setData(Uri.parse("scott://com.scott.intent.data:/target"));
startActivity(intent);
}
此時,url中的每個部分和TargetActivity配置信息中全部一致才能跳轉(zhuǎn)成功,否則就被系統(tǒng)拒絕。
不過有時候?qū)ath限定死了也不太好,比如我們有這樣的url:(scott://com.scott.intent.data:7788/target/hello)(scott://com.scott.intent.data:7788/target/hi) 這個時候該怎么辦呢? 我們需要使用另外一個元素:android:pathPrefix,表示路徑前綴。 我們把a(bǔ)ndroid:path="/target"修改為android:pathPrefix="/target",然后就可以滿足以上的要求了。 而在進(jìn)行搜索時,我們使用了一個putExtra方法,將關(guān)鍵字做為參數(shù)放置在Intent中,我們成為extras(附加信息),這里面涉及到了一個Bundle對象。
Bundle和Intent有著密不可分的關(guān)系,主要負(fù)責(zé)為Intent保存附加參數(shù)信息,它實(shí)現(xiàn)了android.os.Paracelable接口, 內(nèi)部維護(hù)一個Map類型的屬性,用于以鍵值對的形式存放附加參數(shù)信息。在我們使用Intent的putExtra方法放置附加信息時,該方法會檢查默認(rèn)的 Bundle實(shí)例為不為空,如果為空,則新創(chuàng)建一個Bundle實(shí)例,然后將具體的參數(shù)信息放置到Bundle實(shí)例中。我們也可以自己創(chuàng)建Bundle對 象,然后為Intent指定這個Bundle即可,如下:
public void gotoTargetActivity(View view) {
Intent intent = new Intent("com.scott.intent.action.TARGET");
Bundle bundle = new Bundle();
bundle.putInt("id", );
bundle.putString("name", "scott");
intent.putExtras(bundle);
startActivity(intent);
}
需要注意的是,在使用putExtras方法設(shè)置Bundle對象之后,系統(tǒng)進(jìn)行的不是引用操作,而是復(fù)制操作,所以如果設(shè)置完之后再更改bundle實(shí) 例中的數(shù)據(jù),將不會影響Intent內(nèi)部的附加信息。那我們?nèi)绾潍@取設(shè)置在Intent中的附加信息呢?與之對應(yīng)的是,我們要從Intent中獲取到 Bundle實(shí)例,然后再從中取出對應(yīng)的鍵值信息:
Bundle bundle = intent.getExtras();
int id = bundle.getInt("id");
String name = bundle.getString("name");
當(dāng)然我們也可以使用Intent的getIntExtra和getStringExtra方法獲取,其數(shù)據(jù)源都是Intent中的Bundle類型的實(shí)例對象。
3.category,要執(zhí)行動作的目標(biāo)所具有的特質(zhì)或行為歸類
例如:在我們的應(yīng)用主界面Activity通常有如下配置:
<category android:name="android.intent.category.LAUNCHER" />
代表該目標(biāo)Activity是該應(yīng)用所在task中的初始Activity并且出現(xiàn)在系統(tǒng)launcher的應(yīng)用列表中。
幾個常見的category如下:
在為Intent設(shè)置category時,應(yīng)使用addCategory(String category)方法向Intent中添加指定的類別信息,來匹配聲明了此類別的目標(biāo)Activity。
下面舉一個回到Home界面的例子:
main.xml:
<?xml version="." encoding="utf-"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="測試Intent Category" /> <Button android:id="@+id/Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="轉(zhuǎn)到Home界面" /> </LinearLayout>
strings.xml:
<?xml version="." encoding="utf-"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">IntentCategoryDemo</string> </resources>
MainActivity.java:
package com.android.category.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.Button);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);// 添加Action屬性
intent.addCategory(Intent.CATEGORY_HOME);// 添加Category屬性
startActivity(intent);// 啟動Activity
}
});
}
}
效果圖如下:

Home:

4.type:要執(zhí)行動作的目標(biāo)Activity所能處理的MIME數(shù)據(jù)類型
例如:一個可以處理圖片的目標(biāo)Activity在其聲明中包含這樣的mimeType:
<data android:mimeType="image/*" />
在使用Intent進(jìn)行匹配時,我們可以使用setType(String type)或者setDataAndType(Uri data, String type)來設(shè)置mimeType。
5.component,目標(biāo)組件的包或類名稱
在使用component進(jìn)行匹配時,一般采用以下幾種形式:
intent.setComponent(new ComponentName(getApplicationContext(), TargetActivity.class));
intent.setComponent(new ComponentName(getApplicationContext(), "com.scott.intent.TargetActivity"));
intent.setComponent(new ComponentName("com.scott.other", "com.scott.other.TargetActivity"));
其中,前兩種是用于匹配同一包內(nèi)的目標(biāo),第三種是用于匹配其他包內(nèi)的目標(biāo)。
【注意】:如果我們在Intent中指定了component屬性,系統(tǒng)將不會再對action、data/type、category進(jìn)行匹配。
五、Intent用法示例全面總結(jié):
1. 調(diào)用撥號程序
// 給移動客服撥打電話
Uri uri = Uri.parse("tel:");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);
2.發(fā)送短信或彩信
// 給發(fā)送內(nèi)容為“Hello”的短信
Uri uri = Uri.parse("smsto:");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "Hello");
startActivity(intent);
// 發(fā)送彩信(相當(dāng)于發(fā)送帶附件的短信)
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "Hello");
Uri uri = Uri.parse("content://media/external/images/media/");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
startActivity(intent);
3.通過瀏覽器打開網(wǎng)頁
// 打開Google主頁
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
.發(fā)送電子郵件
// 給someone@domain.com發(fā)郵件
Uri uri = Uri.parse("mailto:someone@domain.com");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(intent);
// 給someone@domain.com發(fā)郵件發(fā)送內(nèi)容為“Hello”的郵件
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, "someone@domain.com");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Hello");
intent.setType("text/plain");
startActivity(intent);
// 給多人發(fā)郵件
Intent intent=new Intent(Intent.ACTION_SEND);
String[] tos = {"@abc.com", "@abc.com"}; // 收件人
String[] ccs = {"@abc.com", "@abc.com"}; // 抄送
String[] bccs = {"@abc.com", "@abc.com"}; // 密送
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_BCC, bccs);
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Hello");
intent.setType("message/rfc");
startActivity(intent);
5.顯示地圖與路徑規(guī)劃
// 打開Google地圖中國北京位置(北緯.,東經(jīng).)
Uri uri = Uri.parse("geo:.,.");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
// 路徑規(guī)劃:從北京某地(北緯.,東經(jīng).)到上海某地(北緯.,東經(jīng).)
Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=. .&daddr=. .");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
6. 播放多媒體
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/foo.mp");
intent.setDataAndType(uri, "audio/mp");
startActivity(intent);
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
7. 拍照
// 打開拍照程序
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, );
// 取出照片數(shù)據(jù)
Bundle extras = intent.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
8.獲取并剪切圖片
// 獲取并剪切圖片
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.putExtra("crop", "true"); // 開啟剪切
intent.putExtra("aspectX", ); // 剪切的寬高比為:
intent.putExtra("aspectY", );
intent.putExtra("outputX", ); // 保存圖片的寬和高
intent.putExtra("outputY", );
intent.putExtra("output", Uri.fromFile(new File("/mnt/sdcard/temp"))); // 保存路徑
intent.putExtra("outputFormat", "JPEG");// 返回格式
startActivityForResult(intent, );
// 剪切特定圖片
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setClassName("com.android.camera", "com.android.camera.CropImage");
intent.setData(Uri.fromFile(new File("/mnt/sdcard/temp")));
intent.putExtra("outputX", ); // 剪切的寬高比為:
intent.putExtra("outputY", );
intent.putExtra("aspectX", ); // 保存圖片的寬和高
intent.putExtra("aspectY", );
intent.putExtra("scale", true);
intent.putExtra("noFaceDetection", true);
intent.putExtra("output", Uri.parse("file:///mnt/sdcard/temp"));
startActivityForResult(intent, );
9. 打開Google Market
// 打開Google Market直接進(jìn)入該程序的詳細(xì)頁面
Uri uri = Uri.parse("market://details?id=" + "com.demo.app");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
10.安裝和卸載程序
Uri uri = Uri.fromParts("package", "com.demo.app", null);
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
startActivity(intent);
11. 進(jìn)入設(shè)置界面
// 進(jìn)入無線網(wǎng)絡(luò)設(shè)置界面(其它可以舉一反三) Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); startActivityForResult(intent, );
- Android 幾種屏幕間跳轉(zhuǎn)的跳轉(zhuǎn)Intent Bundle
- android中Intent傳值與Bundle傳值的區(qū)別詳解
- Android Activity中使用Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)與參數(shù)傳遞的方法
- Android利用Intent實(shí)現(xiàn)記事本功能(NotePad)
- Android利用Intent啟動和關(guān)閉Activity
- Android利用Intent讀取和更新通訊錄
- Android利用Intent實(shí)現(xiàn)讀取圖片操作
- 詳解Android中通過Intent類實(shí)現(xiàn)組件間調(diào)用的方法
- Android 通過Intent使用Bundle傳遞對象詳細(xì)介紹
相關(guān)文章
Android Studio設(shè)置顏色拾色器工具Color Picker教程
這篇文章主要介紹了Android Studio設(shè)置顏色拾色器工具Color Picker教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android使用Handler實(shí)現(xiàn)定時器與倒計(jì)時器功能
Handler的最常見應(yīng)用場景之一便是通過Handler在子線程中間接更新UI。這篇文章主要介紹了Android使用Handler實(shí)現(xiàn)定時器與倒計(jì)時器功能,需要的朋友可以參考下2018-02-02
Android編程實(shí)現(xiàn)的EditText彈出打開和關(guān)閉工具類
這篇文章主要介紹了Android編程實(shí)現(xiàn)的EditText彈出打開和關(guān)閉工具類,涉及Android輸入框EditText彈出打開和關(guān)閉功能簡單實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-02-02
Android手機(jī)獲取root權(quán)限并實(shí)現(xiàn)關(guān)機(jī)重啟功能的方法
這篇文章主要介紹了Android手機(jī)獲取root權(quán)限并實(shí)現(xiàn)關(guān)機(jī)重啟功能的方法,是Android程序設(shè)計(jì)中非常重要的技巧,需要的朋友可以參考下2014-08-08
Android通過繼承Binder類實(shí)現(xiàn)多進(jìn)程通信
本篇文章主要介紹了Android通過繼承Binder類實(shí)現(xiàn)多進(jìn)程通信,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03
Android自定義Drawable實(shí)現(xiàn)圓形和圓角
這篇文章主要為大家詳細(xì)介紹了Android自定義Drawable實(shí)現(xiàn)圓形和圓角,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09

