最近在進行android開發過程當中,在將 Intent傳遞給調用的組件並完成組件的調用時遇到點困難,而且以前對Intent的學習也是隻知其一;不知其二,最近特地爲此拿出一些時間,對Intent部分進行了系統的學習並進行了部分實踐,下面將本身的學習及Intent知識進行了詳細的概括整理,但願能幫助到一樣遇到相同問題的博友。下面是Intent介紹、詳解及Intent示例總結:java
一.Intent介紹:android
Intent的中文意思是「意圖,意向」,在Android中提供了Intent機制來協助應用間的交互與通信,Intent負責對應用中一次操做的動 做、動做涉及數據、附加數據進行描述,Android則根據此Intent的描述,負責找到對應的組件,將 Intent傳遞給調用的組件,並完成組件的調用。Intent不只可用於應用程序之間,也可用於應用程序內部的Activity/Service之間的 交互。所以,能夠將Intent理解爲不一樣組件之間通訊的「媒介」專門提供組件互相調用的相關信息。瀏覽器
2、Intent做用:網絡
Intent 是一個將要執行的動做的抽象的描述,通常來講是做爲參數來使用,由Intent來協助完成android各個組件之間的通信。好比說調用 startActivity()來啓動一個activity,或者由broadcaseIntent()來傳遞給全部感興趣的 BroadcaseReceiver, 再或者由startService()/bindservice()來啓動一個後臺的service.因此能夠看出來,intent主要是用來啓動其餘的 activity 或者service,因此能夠將intent理解成activity之間的粘合劑。 app
三.Inten啓動組件的方法:dom
Intent能夠啓動一個Activity,也能夠啓動一個Service,還能夠發起一個廣播Broadcasts。具體方法以下:ide
組件名稱學習 |
方法名稱測試 |
Activity搜索引擎 |
startActvity( ) startActivity( ) |
Service |
startService( ) bindService( ) |
Broadcasts |
sendBroadcasts( ) sendOrderedBroadcasts( ) sendStickyBroadcasts( ) |
四.Intent的幾個重要屬性,下面進行詳解:
動做(Action),數據(Data),分類(Category),類型(Type),組件(Compent)以及擴展信(Extra)。其中最經常使用的是Action屬性和Data屬性。
1.Action屬性:
對於有以下聲明的Activity:
1 <activity android:name=".TargetActivity">
2 <intent-filter> 3 <action android:name="com.scott.intent.action.TARGET"/> 4 <category android:name="android.intent.category.DEFAULT"/> 5 </intent-filter> 6 </activity>
TargetActivity在其<intent-filter>中聲明瞭<action>,即目標action,若是咱們須要作一個跳轉的動做,就須要在Intent中指定目標的action,以下:
1 public void gotoTargetActivity(View view) { 2 Intent intent = new Intent("com.scott.intent.action.TARGET"); 3 startActivity(intent); 4 }
當咱們爲Intent指定相應的action,而後調用startActivity方法後,系統會根據action跳轉到對應的Activity。
除了自定義的action以外,Intent也內含了不少默認的action,下面列舉幾個:
1 public static final String ACTION_MAIN = "android.intent.action.MAIN"; 2 public static final String ACTION_VIEW = "android.intent.action.VIEW"; 3 public static final String ACTION_WEB_SEARCH = "android.intent.action.WEB_SEARCH"; 4 public static final String ACTION_CALL = "android.intent.action.CALL";
每個action都有其特定的用途。
2.data和extras,即執行動做要操做的數據和傳遞到目標的附加信息:
下面舉一個與瀏覽器交互的例子:
1 /** 2 * 打開指定網頁 3 * @param view 4 */ 5 public void invokeWebBrowser(View view) { 6 Intent intent = new Intent(Intent.ACTION_VIEW); 7 intent.setData(Uri.parse("http://www.google.com.hk")); 8 startActivity(intent); 9 } 10 /** 11 * 進行關鍵字搜索 12 * @param view 13 */ 14 public void invokeWebSearch(View view) { 15 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); 16 intent.putExtra(SearchManager.QUERY, "android"); //關鍵字 17 startActivity(intent); 18 }
上面兩種方法分別是啓動瀏覽器並打開指定網頁、進行關鍵字搜索,分別對應的action是Intent.ACTION_VIEW和 Intent.ACTION_WEB_SEARCH,前者需指定相應的網頁地址,後者需指定關鍵字信息,對於關鍵字搜索來講,瀏覽器會按照本身設置的默認 的搜索引擎進行搜索。
咱們注意到,在打開網頁時,爲Intent指定一個data屬性,這實際上是指定要操做的數據,是一個URI的形式,咱們能夠將一個指定前綴的字符串轉換成 特定的URI類型,如:「http:」或「https:」表示網絡地址類型,「tel:」表示電話號碼類型,「mailto:」表示郵件地址類型,等等。
例如,咱們要呼叫給定的號碼,能夠這樣作:
1 public void call(View view) { 2 Intent intent = new Intent(Intent.ACTION_CALL); 3 intent.setData(Uri.parse("tel:12345678")); 4 startActivity(intent); 5 }
那麼咱們如何知道目標是否接受這種前綴呢?這就須要看一下目標中<data/>元素的匹配規則了。
在目標<data/>標籤中包含了如下幾種子元素,他們定義了url的匹配規則:
android:scheme 匹配url中的前綴,除了「http」、「https」、「tel」...以外,咱們能夠定義本身的前綴
android:host 匹配url中的主機名部分,如「google.com」,若是定義爲「*」則表示任意主機名
android:port 匹配url中的端口
android:path 匹配url中的路徑
咱們改動一下TargetActivity的聲明信息:
1 <activity android:name=".TargetActivity">
2 <intent-filter> 3 <action android:name="com.scott.intent.action.TARGET"/> 4 <category android:name="android.intent.category.DEFAULT"/> 5 <data android:scheme="scott" android:host="com.scott.intent.data" android:port="7788" android:path="/target"/> 6 </intent-filter> 7 </activity>
這個時候若是隻指定action就不夠了,咱們須要爲其設置data值,以下:
1 public void gotoTargetActivity(View view) { 2 Intent intent = new Intent("com.scott.intent.action.TARGET"); 3 intent.setData(Uri.parse("scott://com.scott.intent.data:7788/target")); 4 startActivity(intent); 5 }
此時,url中的每一個部分和TargetActivity配置信息中所有一致才能跳轉成功,不然就被系統拒絕。
不過有時候對path限定死了也不太好,好比咱們有這樣的url:(scott://com.scott.intent.data:7788/target/hello)(scott://com.scott.intent.data:7788/target/hi) 這個時候該怎麼辦呢? 咱們須要使用另一個元素:android:pathPrefix,表示路徑前綴。 咱們把android:path="/target"修改成android:pathPrefix="/target",而後就能夠知足以上的要求了。 而在進行搜索時,咱們使用了一個putExtra方法,將關鍵字作爲參數放置在Intent中,咱們成爲extras(附加信息),這裏面涉及到了一個Bundle對象。
Bundle和Intent有着密不可分的關係,主要負責爲Intent保存附加參數信息,它實現了android.os.Paracelable接口, 內部維護一個Map類型的屬性,用於以鍵值對的形式存放附加參數信息。在咱們使用Intent的putExtra方法放置附加信息時,該方法會檢查默認的 Bundle實例爲不爲空,若是爲空,則新建立一個Bundle實例,而後將具體的參數信息放置到Bundle實例中。咱們也能夠本身建立Bundle對 象,而後爲Intent指定這個Bundle便可,以下:
1 public void gotoTargetActivity(View view) { 2 Intent intent = new Intent("com.scott.intent.action.TARGET"); 3 Bundle bundle = new Bundle(); 4 bundle.putInt("id", 0); 5 bundle.putString("name", "scott"); 6 intent.putExtras(bundle); 7 startActivity(intent); 8 }
須要注意的是,在使用putExtras方法設置Bundle對象以後,系統進行的不是引用操做,而是複製操做,因此若是設置完以後再更改bundle實 例中的數據,將不會影響Intent內部的附加信息。那咱們如何獲取設置在Intent中的附加信息呢?與之對應的是,咱們要從Intent中獲取到 Bundle實例,而後再從中取出對應的鍵值信息:
1 Bundle bundle = intent.getExtras(); 2 int id = bundle.getInt("id"); 3 String name = bundle.getString("name");
固然咱們也可使用Intent的getIntExtra和getStringExtra方法獲取,其數據源都是Intent中的Bundle類型的實例對象。
3.category,要執行動做的目標所具備的特質或行爲歸類
例如:在咱們的應用主界面Activity一般有以下配置:
1 <category android:name="android.intent.category.LAUNCHER" />
表明該目標Activity是該應用所在task中的初始Activity而且出如今系統launcher的應用列表中。
幾個常見的category以下:
常量 |
解釋 |
CATEGORY_DEFAULT |
默認的category |
CATEGORY_BROWSABLE |
指定了此category後,在網頁上點擊圖片或連接時,系統會考慮將此目標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 |
表示該目標Activity是一個首選項界面; |
在爲Intent設置category時,應使用addCategory(String category)方法向Intent中添加指定的類別信息,來匹配聲明瞭此類別的目標Activity。
下面舉一個回到Home界面的例子:
main.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 > 6 <TextView 7 android:layout_width="fill_parent" 8 android:layout_height="wrap_content" 9 android:text="測試Intent Category" 10 /> 11 <Button 12 android:id="@+id/Button1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="轉到Home界面" 16 /> 17 </LinearLayout>
strings.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <resources> 3 <string name="hello">Hello World, MainActivity!</string> 4 <string name="app_name">IntentCategoryDemo</string> 5 </resources>
MainActivity.java:
1 package com.android.category.activity; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 10 public class MainActivity extends Activity { 11 private Button btn; 12 @Override 13 public void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.main); 16 17 btn = (Button)findViewById(R.id.Button1); 18 btn.setOnClickListener(new OnClickListener() { 19 @Override 20 public void onClick(View v) { 21 Intent intent = new Intent(); 22 intent.setAction(Intent.ACTION_MAIN);// 添加Action屬性 23 intent.addCategory(Intent.CATEGORY_HOME);// 添加Category屬性 24 startActivity(intent);// 啓動Activity 25 } 26 }); 27 } 28 }
效果圖以下:
Home:
4.type:要執行動做的目標Activity所能處理的MIME數據類型
例如:一個能夠處理圖片的目標Activity在其聲明中包含這樣的mimeType:
1 <data android:mimeType="image/*" />
在使用Intent進行匹配時,咱們可使用setType(String type)或者setDataAndType(Uri data, String type)來設置mimeType。
5.component,目標組件的包或類名稱
在使用component進行匹配時,通常採用如下幾種形式:
1 intent.setComponent(new ComponentName(getApplicationContext(), TargetActivity.class)); 2 intent.setComponent(new ComponentName(getApplicationContext(), "com.scott.intent.TargetActivity")); 3 intent.setComponent(new ComponentName("com.scott.other", "com.scott.other.TargetActivity"));
其中,前兩種是用於匹配同一包內的目標,第三種是用於匹配其餘包內的目標。
【注意】:若是咱們在Intent中指定了component屬性,系統將不會再對action、data/type、category進行匹配。
5、Intent用法示例全面總結:
1. 調用撥號程序
1 // 給移動客服10086撥打電話 2 Uri uri = Uri.parse("tel:10086"); 3 Intent intent = new Intent(Intent.ACTION_DIAL, uri); 4 startActivity(intent);
2.發送短信或彩信
1 // 給10086發送內容爲「Hello」的短信 2 Uri uri = Uri.parse("smsto:10086"); 3 Intent intent = new Intent(Intent.ACTION_SENDTO, uri); 4 intent.putExtra("sms_body", "Hello"); 5 startActivity(intent);
1 // 發送彩信(至關於發送帶附件的短信) 2 3 Intent intent = new Intent(Intent.ACTION_SEND); 4 intent.putExtra("sms_body", "Hello"); 5 Uri uri = Uri.parse("content://media/external/images/media/23"); 6 intent.putExtra(Intent.EXTRA_STREAM, uri); 7 intent.setType("image/png"); 8 startActivity(intent);
3.經過瀏覽器打開網頁
1 // 打開Google主頁 2 Uri uri = Uri.parse("http://www.google.com"); 3 Intent intent = new Intent(Intent.ACTION_VIEW, uri); 4 startActivity(intent);
4.發送電子郵件
1 // 給someone@domain.com發郵件 2 Uri uri = Uri.parse("mailto:someone@domain.com"); 3 Intent intent = new Intent(Intent.ACTION_SENDTO, uri); 4 startActivity(intent);
1 // 給someone@domain.com發郵件發送內容爲「Hello」的郵件 2 Intent intent = new Intent(Intent.ACTION_SEND); 3 intent.putExtra(Intent.EXTRA_EMAIL, "someone@domain.com"); 4 intent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); 5 intent.putExtra(Intent.EXTRA_TEXT, "Hello"); 6 intent.setType("text/plain"); 7 startActivity(intent);
1 // 給多人發郵件 2 Intent intent=new Intent(Intent.ACTION_SEND); 3 String[] tos = {"1@abc.com", "2@abc.com"}; // 收件人 4 String[] ccs = {"3@abc.com", "4@abc.com"}; // 抄送 5 String[] bccs = {"5@abc.com", "6@abc.com"}; // 密送 6 intent.putExtra(Intent.EXTRA_EMAIL, tos); 7 intent.putExtra(Intent.EXTRA_CC, ccs); 8 intent.putExtra(Intent.EXTRA_BCC, bccs); 9 intent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); 10 intent.putExtra(Intent.EXTRA_TEXT, "Hello"); 11 intent.setType("message/rfc822"); 12 startActivity(intent);
5.顯示地圖與路徑規劃
1 // 打開Google地圖中國北京位置(北緯39.9,東經116.3) 2 Uri uri = Uri.parse("geo:39.9,116.3"); 3 Intent intent = new Intent(Intent.ACTION_VIEW, uri); 4 startActivity(intent);
1 // 路徑規劃:從北京某地(北緯39.9,東經116.3)到上海某地(北緯31.2,東經121.4) 2 Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=39.9 116.3&daddr=31.2 121.4"); 3 Intent intent = new Intent(Intent.ACTION_VIEW, uri); 4 startActivity(intent);
6. 播放多媒體
1 Intent intent = new Intent(Intent.ACTION_VIEW); 2 Uri uri = Uri.parse("file:///sdcard/foo.mp3"); 3 intent.setDataAndType(uri, "audio/mp3"); 4 startActivity(intent); 5 6 Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); 7 Intent intent = new Intent(Intent.ACTION_VIEW, uri); 8 startActivity(intent);
7. 拍照
1 // 打開拍照程序 2 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 3 startActivityForResult(intent, 0);
1 // 取出照片數據 2 Bundle extras = intent.getExtras(); 3 Bitmap bitmap = (Bitmap) extras.get("data");
8.獲取並剪切圖片
1 // 獲取並剪切圖片 2 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 3 intent.setType("image/*"); 4 intent.putExtra("crop", "true"); // 開啓剪切 5 intent.putExtra("aspectX", 1); // 剪切的寬高比爲1:2 6 intent.putExtra("aspectY", 2); 7 intent.putExtra("outputX", 20); // 保存圖片的寬和高 8 intent.putExtra("outputY", 40); 9 intent.putExtra("output", Uri.fromFile(new File("/mnt/sdcard/temp"))); // 保存路徑 10 intent.putExtra("outputFormat", "JPEG");// 返回格式 11 startActivityForResult(intent, 0);
1 // 剪切特定圖片 2 3 1 Intent intent = new Intent("com.android.camera.action.CROP"); 4 2 intent.setClassName("com.android.camera", "com.android.camera.CropImage"); 5 3 intent.setData(Uri.fromFile(new File("/mnt/sdcard/temp"))); 6 4 intent.putExtra("outputX", 1); // 剪切的寬高比爲1:2 7 5 intent.putExtra("outputY", 2); 8 6 intent.putExtra("aspectX", 20); // 保存圖片的寬和高 9 7 intent.putExtra("aspectY", 40); 10 8 intent.putExtra("scale", true); 11 9 intent.putExtra("noFaceDetection", true); 12 10 intent.putExtra("output", Uri.parse("file:///mnt/sdcard/temp")); 13 11 startActivityForResult(intent, 0);
9. 打開Google Market
1 // 打開Google Market直接進入該程序的詳細頁面 2 Uri uri = Uri.parse("market://details?id=" + "com.demo.app"); 3 Intent intent = new Intent(Intent.ACTION_VIEW, uri); 4 startActivity(intent);
10.安裝和卸載程序
1 Uri uri = Uri.fromParts("package", "com.demo.app", null); 2 Intent intent = new Intent(Intent.ACTION_DELETE, uri); 3 startActivity(intent);
11. 進入設置界面
1 // 進入無線網絡設置界面(其它能夠觸類旁通) 2 Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); 3 startActivityForResult(intent, 0);