一磷雇、Intent
An intent is an abstract description of an operation to be performed. It can be used with
Context#startActivity(Intent)
to launch anActivity
,broadcastIntent
to send it to any interestedBroadcastReceiver
components, andContext.startService(Intent)
orContext.bindService(Intent, ServiceConnection, int)
to communicate with a backgroundService
.
Intent-Android Developers
二请契、Intent的分類
-
顯式Intent:
Intent有多個構造函數(shù)的重載,其中一個是Intent(Context packageContext, Class<?>cls)巢音,第一個參數(shù)Context 要求提供一個啟動活動的上下文划址,第二個參數(shù)Class 則是指定想要啟動的目標活動使用蛀骇。
通過startActivity(Intent)方式來啟動活動和泌,Intent的“意圖”非常明顯评汰,稱之為顯式Intent
。
-
隱式Intent:
隱式Intent
并不明確指出我們想要啟動哪一個活動痢虹,而是指定了一系列更為抽象的action 和category 等信息被去,然后交由系統(tǒng)去分析這個Intent,并幫我們找出合適的活動去啟動奖唯。
三惨缆、Intent的屬性
Intent對象大致包括7大屬性:
Action(動作)
、Data(數(shù)據(jù))
丰捷、Category(類別)
坯墨、Type(數(shù)據(jù)類型)
、Component(組件)
病往、Extra(擴展信息)
捣染、Flag(標志位)
1、主要屬性(Action
,Data
,category
)
-
Action
The general action to be performed
用來表現(xiàn)意圖的行動的一個字符串變量停巷,可以用來指定Intent要執(zhí)行的動作類別耍攘。
1、Standard Activity Actions
動作名稱 | 作用 |
---|---|
ACTION_MAIN |
表示程序入口 |
ACTION_VIEW |
自動以最合適的方式顯示Data |
ACTION_EDIT |
提供可以編輯的 |
ACTION_PICK |
選擇一個一條Data畔勤,并且返回它 |
ACTION_DAIL |
顯示Data指向的號碼在撥號界面Dailer上 |
ACTION_CALL |
撥打Data指向的號碼 |
ACTION_SEND |
發(fā)送Data到指定的地方 |
ACTION_SENDTO |
發(fā)送多組Data到指定的地方 |
ACTION_RUN |
運行Data蕾各,不管Data是什么 |
ACTION_SEARCH |
執(zhí)行搜索 |
ACTION_WEB_SEARCH |
執(zhí)行網(wǎng)上搜索 |
ACRION_SYNC |
執(zhí)同步一個Data |
ACTION_INSERT |
添加一個空的項到容器中 |
2、Standard Broadcast Actions
動作名稱 | 作用 |
---|---|
ACTION_TIME_TICK |
當前時間改變庆揪,并即時發(fā)送時間式曲,只能通過系統(tǒng)發(fā)送。調用格式"android.intent.action.TIME_TICK"
|
ACTION_TIME_CHENGED |
設置時間嚷硫。調用格式"android.intent.action.TIME_SET"
|
-
Data
The data to operate on, such as a person record in the contacts database, expressed as a Uri
表示與動作要操縱的數(shù)據(jù)检访。一個URI對象是一個引用的data的表現(xiàn)形式始鱼,或是data的MIME類型仔掸;data的類型由Intent的action決定。
-
Categories
Gives additional information about the action to execute. For example, CATEGORY_LAUNCHER
means it should appear in the Launcher as a top-level application, while CATEGORY_ALTERNATIVE
means it should be included in a list of alternative actions the user can perform on a piece of data.
方法addCategory(String)
用來表現(xiàn)動作的類別医清。一個包含Intent額外信息的字符串起暮,表示哪種類型的組件來處理這個Intent。任何數(shù)量的Category 描述都可以添加到Intent中会烙,但是很多intent不需要category(默認為Default)负懦,下面列舉一些常用的category:
類型 | 作用 |
---|---|
CATEGORY_DEFAULT |
把一個組件Component設為可被implicit啟動的 |
CATEGORY_LAUNCHER |
把一個action設置為在頂級執(zhí)行。并且包含這個屬性的Activity所定義的icon將取代application中定義的icon |
CATEGORY_BROWSABLE |
當Intent指向網(wǎng)絡相關時柏腻,必須要添加這個類別 |
CATEGORY_HOME |
使Intent指向Home界面 |
CATEGORY_PREFERENCE |
定義的Activity是一個偏好面板Preference Panel |
2纸厉、次要屬性(Type
、Component
五嫂、Extra
颗品、Flag
)
-
Type
Specifies an explicit type (a MIME type) of the intent data. Normally the type is inferred from the data itself. By setting this attribute, you disable that evaluation and force an explicit type
指定數(shù)據(jù)類型肯尺。 顯示的指定Intent的數(shù)據(jù)類型,而跳過了通過Uri進行推導判斷的步驟躯枢。如:Vedio则吟、Image、Voice等锄蹂。
-
Component
Specifies an explicit name of a component class to use for the intent. Normally this is determined by looking at the other information in the intent (the action, data/type, and categories) and matching that with a component that can handle it. If this attribute is set then none of the evaluation is performed, and this component is used exactly as is. By specifying this attribute, all of the other Intent attributes become optional.
目的組件氓仲。 指定Intent的目標組件名稱,當指定了這個屬性后得糜,系統(tǒng)將跳過匹配其他屬性敬扛,而直接匹配這個屬性來啟動對應的組件。
-
Extra
This is a Bundle
of any additional information. This can be used to provide extended information to the component. For example, if we have a action to send an e-mail message, we could also include extra pieces of data here to supply a subject, body, etc.
擴展信息朝抖。Intent可以攜帶的額外 key-value
數(shù)據(jù)舔哪,你可以通過調用putExtra()
方法設置數(shù)據(jù),每一個 key對應一個 value數(shù)據(jù)槽棍。你也可以通過創(chuàng)建 Bundle對象來存儲所有數(shù)據(jù)捉蚤,然后通過調用putExtras()
方法來設置數(shù)據(jù)。
類型 | 作用 |
---|---|
EXTRA_BCC |
存放郵件密送人地址的字符串數(shù)組 |
EXTRA_CC |
存放郵件抄送人地址的字符串數(shù)組 |
EXTRA_EMAIL |
存放郵件地址的字符串數(shù)組 |
EXTRA_SUBJECT |
存放郵件主題字符串 |
EXTRA_TEXT |
存放郵件內容 |
EXTRA_KEY_EVENT |
以KeyEvent對象方式存放觸發(fā)Intent 的按鍵 |
EXTRA_PHONE_ NUMBER |
存放調用ACTION_CALL 時的電話號碼 |
-
Flag
意圖的運行模式炼七。用來指示系統(tǒng)如何啟動一個Activity缆巧,可以通過setFlags()
或者addFlags()
可以把標簽flag
用在Intent中。
類型 | 作用 |
---|---|
FLAG_ACTIVITY_CLEAR_TOP |
相當于SingleTask |
FLAGE_ACTIVITY_SINGLE_TOP |
相當于SingleTop |
FLAG_ACTIVITY_NEW_TASK |
類似于SingleInstance |
FLAG_ACTIVITY_NO_HISTORY |
當離開該Activity后豌拙,該Activity將被從任務棧中移除 |
三陕悬、Intent的常用方法
1、構造方法:
Intent(Intent intent); //通過復制一個Intent來創(chuàng)建一個新的Intent
Intent(String action); //構造一個指定action的Intent對象
Intent(String action按傅,Uri uri); //構造一個指定action捉超,并且有指向數(shù)據(jù)的uri的Intent對象
Intent(Context packageContext,Clsaa<?>class); //構造一個有指定Component屬性的Intent
2唯绍、常用方法:
addCategory(String Category); //添加一條Category屬性
addFlags(int Flags); //添加一條Flag標簽
clone(); //復制一個Object類型的對象
getExtras(); //獲得一個Bundle對象
getSourceBounds(); //獲得發(fā)送Intent的組件的邊界坐標
makeMainActivity(ComponentName mainActivity); //創(chuàng)建一個Category屬性為luancher的Intent去啟動mainActivity
parseUri(String uri拼岳,int flags) //通過解析一個uri獲得一個Intent對象
setComponent(); //添加component屬性
setClass(); //相當于添加component屬性
setClassName(); //相當于添加component屬性
resolveActivity(PackageManager); //解析Intent啟動的Activity,返回ComponentName對象
createChooser(Intent target况芒,CharSequence title);// 創(chuàng)建一個有選擇對話框的Intent
setDataAndType(Uri data, String type)//Uri類型的data和Mime類型的type
setData();setType();
getData();getType();//設置/獲得Data/Type類型的數(shù)據(jù)
3惜纸、在AndroidManifest中配置:
通過在<activity> 標簽下配置<intent-filter> 的內容,可以指定當前活動能夠響應的action 和category
<activity android:name=".SecondActivity" >
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.activitytest.MY_CATEGORY"/>
<data android:scheme="http"/>
</intent-filter>
</activity>
可以在<intent-filter> 標簽中再配置一個<data> 標簽绝骚,用于更精確地指定當前活動能夠響應什么類型的數(shù)據(jù)耐版。<data> 標簽中主要可以配置以下內容。
- android:scheme 粪牲。用于指定數(shù)據(jù)的協(xié)議部分止剖,如http湿滓。
- android:host 。用于指定數(shù)據(jù)的主機名部分舌狗,如www.baidu.com叽奥。
- android:port 痛侍。用于指定數(shù)據(jù)的端口部分,一般緊隨在主機名之后主届。
- android:path 赵哲。用于指定主機名和端口之后的部分,如一段網(wǎng)址中跟在域名之后的內容枫夺。
- android:mimeType 绘闷。用于指定可以處理的數(shù)據(jù)類型,允許使用通配符的方式進行指定印蔗。
四、常用系統(tǒng)Intent合集
// 調用撥打電話吧趣,給10010撥打電話
//===============================================================
//1.撥打電話
// 給移動客服10086撥打電話
Uri uri = Uri.parse("tel:10086");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);
//===============================================================
//2.發(fā)送短信
// 給10086發(fā)送內容為“Hello”的短信
Uri uri = Uri.parse("smsto:10086");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "Hello");
startActivity(intent);
//3.發(fā)送彩信(相當于發(fā)送帶附件的短信)
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "Hello");
Uri uri = Uri.parse("content://media/external/images/media/23");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
startActivity(intent);
//===============================================================
//4.打開瀏覽器:
// 打開百度主頁
Uri uri = Uri.parse("http://www.baidu.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
//===============================================================
//5.發(fā)送電子郵件:(閹割了Google服務的沒戲!!!!)
// 給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ā)送內容為“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 = {"1@abc.com", "2@abc.com"}; // 收件人
String[] ccs = {"3@abc.com", "4@abc.com"}; // 抄送
String[] bccs = {"5@abc.com", "6@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/rfc822");
startActivity(intent);
//===============================================================
//6.顯示地圖:
// 打開Google地圖中國北京位置(北緯39.9强挫,東經(jīng)116.3)
Uri uri = Uri.parse("geo:39.9,116.3");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
//===============================================================
//7.路徑規(guī)劃
// 路徑規(guī)劃:從北京某地(北緯39.9薛躬,東經(jīng)116.3)到上海某地(北緯31.2,東經(jīng)121.4)
Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=39.9 116.3&daddr=31.2 121.4");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
//===============================================================
//8.多媒體播放:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/foo.mp3");
intent.setDataAndType(uri, "audio/mp3");
startActivity(intent);
//獲取SD卡下所有音頻文件,然后播放第一首=-=
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
//===============================================================
//9.打開攝像頭拍照:
// 打開拍照程序
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
// 取出照片數(shù)據(jù)
Bundle extras = intent.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
//另一種:
//調用系統(tǒng)相機應用程序稠诲,并存儲拍下來的照片
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
time = Calendar.getInstance().getTimeInMillis();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment
.getExternalStorageDirectory().getAbsolutePath()+"/tucue", time + ".jpg")));
startActivityForResult(intent, ACTIVITY_GET_CAMERA_IMAGE);
//===============================================================
//10.獲取并剪切圖片
// 獲取并剪切圖片
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.putExtra("crop", "true"); // 開啟剪切
intent.putExtra("aspectX", 1); // 剪切的寬高比為1:2
intent.putExtra("aspectY", 2);
intent.putExtra("outputX", 20); // 保存圖片的寬和高
intent.putExtra("outputY", 40);
intent.putExtra("output", Uri.fromFile(new File("/mnt/sdcard/temp"))); // 保存路徑
intent.putExtra("outputFormat", "JPEG");// 返回格式
startActivityForResult(intent, 0);
// 剪切特定圖片
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", 1); // 剪切的寬高比為1:2
intent.putExtra("outputY", 2);
intent.putExtra("aspectX", 20); // 保存圖片的寬和高
intent.putExtra("aspectY", 40);
intent.putExtra("scale", true);
intent.putExtra("noFaceDetection", true);
intent.putExtra("output", Uri.parse("file:///mnt/sdcard/temp"));
startActivityForResult(intent, 0);
//===============================================================
//11.打開Google Market
// 打開Google Market直接進入該程序的詳細頁面
Uri uri = Uri.parse("market://details?id=" + "com.demo.app");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
//===============================================================
//12.進入手機設置界面:
// 進入無線網(wǎng)絡設置界面(其它可以舉一反三)
Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
startActivityForResult(intent, 0);
//===============================================================
//13.安裝apk:
Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
//===============================================================
//14.卸載apk:
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
//===============================================================
//15.發(fā)送附件:
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/eoe.mp3");
sendIntent.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));
//===============================================================
//16.進入聯(lián)系人頁面:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(People.CONTENT_URI);
startActivity(intent);
//===============================================================
//17.查看指定聯(lián)系人:
Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, info.id);//info.id聯(lián)系人ID
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(personUri);
startActivity(intent);
//===============================================================
//18.調用系統(tǒng)編輯添加聯(lián)系人(高版本SDK有效):
Intent it = newIntent(Intent.ACTION_INSERT_OR_EDIT);
it.setType("vnd.android.cursor.item/contact");
//it.setType(Contacts.CONTENT_ITEM_TYPE);
it.putExtra("name","myName");
it.putExtra(android.provider.Contacts.Intents.Insert.COMPANY, "organization");
it.putExtra(android.provider.Contacts.Intents.Insert.EMAIL,"email");
it.putExtra(android.provider.Contacts.Intents.Insert.PHONE,"homePhone");
it.putExtra(android.provider.Contacts.Intents.Insert.SECONDARY_PHONE,"mobilePhone");
it.putExtra( android.provider.Contacts.Intents.Insert.TERTIARY_PHONE,"workPhone");
it.putExtra(android.provider.Contacts.Intents.Insert.JOB_TITLE,"title");
startActivity(it);
//===============================================================
//19.調用系統(tǒng)編輯添加聯(lián)系人(全有效):
Intent intent = newIntent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(People.CONTENT_ITEM_TYPE);
intent.putExtra(Contacts.Intents.Insert.NAME, "My Name");
intent.putExtra(Contacts.Intents.Insert.PHONE, "+1234567890");
intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE,Contacts.PhonesColumns.TYPE_MOBILE);
intent.putExtra(Contacts.Intents.Insert.EMAIL, "com@com.com");
intent.putExtra(Contacts.Intents.Insert.EMAIL_TYPE, Contacts.ContactMethodsColumns.TYPE_WORK);
startActivity(intent);
//===============================================================
//20.打開另一程序
Intent i = new Intent();
ComponentName cn = new ComponentName("com.example.jay.test",
"com.example.jay.test.MainActivity");
i.setComponent(cn);
i.setAction("android.intent.action.MAIN");
startActivityForResult(i, RESULT_OK);
//===============================================================
//21.打開錄音機
Intent mi = new Intent(Media.RECORD_SOUND_ACTION);
startActivity(mi);
//===============================================================
//22.從google搜索內容
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY,"searchString")
startActivity(intent);
//===============================================================