Android中提供了Intent機制來協助應用間的交互與通訊,Intent負責對應用中一次操作的動作、動作涉及數據、附加數據進行描述,Android則根據此Intent的描述,負責找到對應的組件,將 Intent傳遞給調用的組件,並完成組件的調用。Intent不僅可用於應用程序之間,也可用於應用程序內部的Activity/Service之間的交互。因此,Intent在這里起着一個媒體中介的作用,專門提供組件互相調用的相關信息,實現調用者與被調用者之間的解耦。
在SDK中給出了Intent作用的表現形式為:
- 通過Context.startActivity() orActivity.startActivityForResult() 啟動一個Activity;
- 通過 Context.startService() 啟動一個服務,或者通過Context.bindService() 和后台服務交互;
- 通過廣播方法(比如 Context.sendBroadcast(),Context.sendOrderedBroadcast(), Context.sendStickyBroadcast()) 發給broadcast receivers。
Intent屬性的設置,包括以下幾點:(以下為XML中定義,當然也可以通過Intent類的方法來獲取和設置)
(1)Action,也就是要執行的動作
SDk中定義了一些標准的動作,包括:
Constant |
Target component |
Action |
ACTION_CALL |
activity |
Initiate a phone call. |
ACTION_EDIT |
activity |
Display data for the user to edit. |
ACTION_MAIN |
activity |
Start up as the initial activity of a task, with no data input and no returned output. |
ACTION_SYNC |
activity |
Synchronize data on a server with data on the mobile device. |
ACTION_BATTERY_LOW |
broadcast receiver |
A warning that the battery is low. |
ACTION_HEADSET_PLUG |
broadcast receiver |
A headset has been plugged into the device, or unplugged from it. |
ACTION_SCREEN_ON |
broadcast receiver |
The screen has been turned on. |
ACTION_TIMEZONE_CHANGED |
broadcast receiver |
The setting for the time zone has changed. |
當然,也可以自定義動作(自定義的動作在使用時,需要加上包名作為前綴,如"com.example.project.SHOW_COLOR”),並可定義相應的Activity來處理我們的自定義動作。
(2)Data,也就是執行動作要操作的數據
Android中采用指向數據的一個URI來表示,如在聯系人應用中,一個指向某聯系人的URI可能為:content://contacts/1。
對於不同的動作,其URI數據的類型是不同的(可以設置type屬性指定特定類型數據),如ACTION_EDIT指定Data為文件URI,打電話為tel:URI,訪問網絡為http:URI,而由content provider提供的數據則為content: URIs。
(3)type(數據類型),顯式指定Intent的數據類型(MIME)。
一般Intent的數據類型能夠根據數據本身進行判定,但是通過設置這個屬性,可以強制采用顯式指定的類型而不再進行推導。
(4)category(類別),被執行動作的附加信息。
例如 LAUNCHER_CATEGORY 表示Intent 的接受者應該在Launcher中作為頂級應用出現;而ALTERNATIVE_CATEGORY表示當前的Intent是一系列的可選動作中的一個,這些動作可以在同一塊數據上執行。
還有其他的為
Constant |
Meaning |
CATEGORY_BROWSABLE |
The target activity can be safely invoked by the browser to display data referenced by a link — for example, an image or an e-mail message. |
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 |
The target activity is a preference panel. |
(5)component(組件),指定Intent的的目標組件的類名稱。
通常 Android會根據Intent 中包含的其它屬性的信息,比如action、data/type、category進行查找,最終找到一個與之匹配的目標組件。
但是,如果 component這個屬性有指定的話,將直接使用它指定的組件,而不再執行上述查找過程。指定了這個屬性以后,Intent的其它所有屬性都是可選的。
(6)extras(附加信息),是其它所有附加信息的集合。
使用extras可以為組件提供擴展信息,比如,如果要執行“發送電子郵件”這個動作,可以將電子郵件的標題、正文等保存在extras里,傳給電子郵件發送組件。
Intent的使用方式:
一、用Action跳轉,隱式Intent
1、使用Action跳轉,如果有一個程序的AndroidManifest.xml中的某一個 Activity的IntentFilter段中定義了包含了相同的Action,那么這個Intent就與這個目標Action匹配。如果這個IntentFilter段中沒有定義Type、Category,那么這個 Activity就匹配了。但是如果手機中有兩個以上的程序匹配,那么就會彈出一個對話可框來提示說明。
Action 的值在Android中有很多預定義,如果你想直接轉到你自己定義的Intent接收者,你可以在接收者的IntentFilter 中加入一個自定義的Action值(同時要設定 Category值為"android.intent.category.DEFAULT"),在你的Intent中設定該值為Intent的 Action,就直接能跳轉到你自己的Intent接收者中。因為這個Action在系統中是唯一的。
2、data/type,你可以用Uri 來作為data,比如
Uri uri = Uri.parse(http://www.google.com);
Intent i = new Intent(Intent.ACTION_VIEW,uri);
手機的Intent分發過程中,會根據http://www.google.com 的scheme判斷出數據類型type 。手機的Brower則能匹配它,在Brower的Manifest.xml中的IntenFilter中首先有ACTION_VIEW Action,也能處理http:的type。
3、至於分類Category,一般不要去在Intent中設置它,如果你寫Intent的接收者,就在Manifest.xml的Activity的IntentFilter中包含android.category.DEFAULT,這樣所有不設置 Category(Intent.addCategory(String c);)的Intent都會與這個Category匹配。
4、extras(附加信息),是其它所有附加信息的集合。使用extras可以為組件提供擴展信息,比如,如果要執行“發送電子郵件”這個動作,可以將電子郵件的標題、正文等保存在extras里,傳給電子郵件發送組件。
隱式的Intent,即Intent的發送者在構造Intent對象時,並不知道也不關心接收者是誰,有利於降低發送者和接收者之間的耦合。
對於顯式Intent,Android不需要去做解析,因為目標組件已經很明確,Android需要解析的是那些隱式Intent,
通過解析,將Intent映射給可以處理此Intent的Activity、IntentReceiver或Service。
Intent解析機制主要是通過查找已注冊在AndroidManifest.xml中的所有IntentFilter及其中定義的Intent,最終找到匹配的Intent。
在這個解析過程中,Android是通過Intent的action、type、category這三個屬性來進行判斷的,判斷方法如下:
- 如果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
Intent傳遞過程中,要找到目標消費者(另一個Activity、IntentReceiver或Service),也就是Intent的響應者。
Intent intent = new Intent();
intent.setClass(context, targetActivy.class);
// 或者用Intent intent = new Intent(context, targetActivity.class);
startActivity(intent);
★intent大全:
1.從google搜索內容
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY,"searchString")
startActivity(intent);
2.瀏覽網頁
Uri uri = Uri.parse("http://www.google.com");
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
3.顯示地圖
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.Action_VIEW,uri);
startActivity(it);
4.路徑規划
Uri uri = Uri.parse("http://maps.google.com/maps?f=dsaddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
5.撥打電話
Uri uri = Uri.parse("tel:xxxxxx");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
6.調用發短信的程序
Intent it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "The SMS text");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
7.發送短信
Uri uri = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);
String body="this is sms demo";
Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", number, null));
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true);
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true);
startActivity(mmsintent);
8.發送彩信
Uri uri = Uri.parse("content://media/external/images/media/23");
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra("sms_body", "some text");
it.putExtra(Intent.EXTRA_STREAM, uri);
it.setType("image/png");
startActivity(it);
StringBuilder sb = new StringBuilder();
sb.append("file://");
sb.append(fd.getAbsoluteFile());
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mmsto", number, null));
// Below extra datas are all optional.
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT, subject);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI, sb.toString());
intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, composeMode);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, exitOnSent);
startActivity(intent);
9.發送Email
Uri uri = Uri.parse("mailto:xxx@abc.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.setType("text/plain");
startActivity(Intent.createChooser(it, "Choose Email Client"));
Intent it=new Intent(Intent.ACTION_SEND);
String[] tos={"me@abc.com"};
String[] ccs={"you@abc.com"};
it.putExtra(Intent.EXTRA_EMAIL, tos);
it.putExtra(Intent.EXTRA_CC, ccs);
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.setType("message/rfc822");
startActivity(Intent.createChooser(it, "Choose Email Client"));
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");
sendIntent.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));
10.播放多媒體
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
11.uninstall apk
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
12.install apk
Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
13. 打開照相機
<1>Intent i = new Intent(Intent.ACTION_CAMERA_BUTTON, null);
this.sendBroadcast(i);
<2>long dateTaken = System.currentTimeMillis();
String name = createName(dateTaken) + ".jpg";
fileName = folder + name;
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, fileName);
values.put("_data", fileName);
values.put(Images.Media.PICASA_ID, fileName);
values.put(Images.Media.DISPLAY_NAME, fileName);
values.put(Images.Media.DESCRIPTION, fileName);
values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, fileName);
Uri photoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent inttPhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
inttPhoto.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(inttPhoto, 10);
14.從gallery選取圖片
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(i, 11);
15. 打開錄音機
Intent mi = new Intent(Media.RECORD_SOUND_ACTION);
startActivity(mi);
16.顯示應用詳細列表
Uri uri = Uri.parse("market://details?id=app_id");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where app_id is the application ID, find the ID
//by clicking on your application on Market home
//page, and notice the ID from the address bar
剛才找app id未果,結果發現用package name也可以
Uri uri = Uri.parse("market://details?id=<packagename>");
這個簡單多了
17尋找應用
Uri uri = Uri.parse("market://search?q=pname:pkg_name");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where pkg_name is the full package path for an application
18打開聯系人列表
<1>
Intent i = new Intent();
i.setAction(Intent.ACTION_GET_CONTENT);
i.setType("vnd.android.cursor.item/phone");
startActivityForResult(i, REQUEST_TEXT);
<2>
Uri uri = Uri.parse("content://contacts/people");
Intent it = new Intent(Intent.ACTION_PICK, uri);
startActivityForResult(it, REQUEST_TEXT);
19 打開另一程序
Intent i = new Intent();
ComponentName cn = new ComponentName("com.yellowbook.android2", "com.yellowbook.android2.AndroidSearch");
i.setComponent(cn);
i.setAction("android.intent.action.MAIN");
startActivityForResult(i, RESULT_OK);
20.調用系統編輯添加聯系人(高版本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);
21.調用系統編輯添加聯系人(全有效):
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);
★intent action大全:
android.intent.action.ALL_APPS
android.intent.action.ANSWER
android.intent.action.ATTACH_DATA
android.intent.action.BUG_REPORT
android.intent.action.CALL
android.intent.action.CALL_BUTTON
android.intent.action.CHOOSER
android.intent.action.CREATE_LIVE_FOLDER
android.intent.action.CREATE_SHORTCUT
android.intent.action.DELETE
android.intent.action.DIAL
android.intent.action.EDIT
android.intent.action.GET_CONTENT
android.intent.action.INSERT
android.intent.action.INSERT_OR_EDIT
android.intent.action.MAIN
android.intent.action.MEDIA_SEARCH
android.intent.action.PICK
android.intent.action.PICK_ACTIVITY
android.intent.action.RINGTONE_PICKER
android.intent.action.RUN
android.intent.action.SEARCH
android.intent.action.SEARCH_LONG_PRESS
android.intent.action.SEND
android.intent.action.SENDTO
android.intent.action.SET_WALLPAPER
android.intent.action.SYNC
android.intent.action.SYSTEM_TUTORIAL
android.intent.action.VIEW
android.intent.action.VOICE_COMMAND
android.intent.action.WEB_SEARCH
android.net.wifi.PICK_WIFI_NETWORK
android.settings.AIRPLANE_MODE_SETTINGS
android.settings.APN_SETTINGS
android.settings.APPLICATION_DEVELOPMENT_SETTINGS
android.settings.APPLICATION_SETTINGS
android.settings.BLUETOOTH_SETTINGS
android.settings.DATA_ROAMING_SETTINGS
android.settings.DATE_SETTINGS
android.settings.DISPLAY_SETTINGS
android.settings.INPUT_METHOD_SETTINGS
android.settings.INTERNAL_STORAGE_SETTINGS
android.settings.LOCALE_SETTINGS
android.settings.LOCATION_SOURCE_SETTINGS
android.settings.MANAGE_APPLICATIONS_SETTINGS
android.settings.MEMORY_CARD_SETTINGS
android.settings.NETWORK_OPERATOR_SETTINGS
android.settings.QUICK_LAUNCH_SETTINGS
android.settings.SECURITY_SETTINGS
android.settings.SETTINGS
android.settings.SOUND_SETTINGS
android.settings.SYNC_SETTINGS
android.settings.USER_DICTIONARY_SETTINGS
android.settings.WIFI_IP_SETTINGS
android.settings.WIFI_SETTINGS
android.settings.WIRELESS_SETTINGS
Intent數據傳遞
啟動四大組件通過Intent對象來實現,Intent的功能包括啟動四大組件以及相關信息+傳遞數據。
其中傳遞數據Intent提供了putExtra和對應的getExtra方法來實現:
putExtra和getExtra 其實是和Bundle put和get方法一一對應的,在Intent類中有一個Bundle的mExtras成員變量
所有的putExtra和getExtra方式實際是調用mExtras對象的put和get方法進行存取。
所以正常情況下 四大組件間傳遞數據直接通過putExtra和getExtra方法存取即可,無需再創建一個bundle對象。
Intent putExtra方法:
Intent putExtra(String name, Bundle value)
Intent putExtra(String name, Parcelable[] value)
Intent putExtra(String name, Serializable value)
Intent putExtra(String name, Parcelable value)
Intent putExtra(String name, int[] value)
Intent putExtra(String name, float value)
Intent putExtra(String name, byte[] value)
Intent putExtra(String name, long[] value)
Intent putExtra(String name, float[] value)
Intent putExtra(String name, long value)
Intent putExtra(String name, String[] value)
Intent putExtra(String name, boolean value)
Intent putExtra(String name, boolean[] value)
Intent putExtra(String name, short value)
Intent putExtra(String name, double value)
Intent putExtra(String name, short[] value)
Intent putExtra(String name, String value)
Intent putExtra(String name, byte value)
Intent putExtra(String name, char[] value)
Intent putExtra(String name, CharSequence[] value)
Intent putExtras(Intent src)
Intent putExtras(Bundle extras)
Intent putIntegerArrayListExtra(String name, ArrayList<Integer> value)
Intent putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)
Intent putStringArrayListExtra(String name, ArrayList<String> value)
Intent getExtra方法:
double[] getDoubleArrayExtra(String name)
double getDoubleExtra(String name, double defaultValue)
Bundle getExtras()
int getFlags()
float[] getFloatArrayExtra(String name)
float getFloatExtra(String name, float defaultValue)
int[] getIntArrayExtra(String name)
int getIntExtra(String name, int defaultValue)
ArrayList<Integer> getIntegerArrayListExtra(String name)
long[] getLongArrayExtra(String name)
long getLongExtra(String name, long defaultValue)
Parcelable[] getParcelableArrayExtra(String name)
<T extends Parcelable> ArrayList<T> getParcelableArrayListExtra(String name)
<T extends Parcelable> T getParcelableExtra(String name)
Serializable getSerializableExtra(String name)
short[] getShortArrayExtra(String name)
short getShortExtra(String name, short defaultValue)
String[] getStringArrayExtra(String name)
ArrayList<String> getStringArrayListExtra(String name)
String getStringExtra(String name)
自定義對象的傳遞:
通過intent 傳遞自定義對象的方法有兩個,第一是實現Serialization接口,第二是實現Parcelable接口。
首選推薦Parceable,android中的很多數據類型都是實現Serialable接口方式來傳遞的,例如 Intent、Bundle、Bitmap、Uri等等。
android Parcelable化的提供了一個接口Parcelable和一個工具類Parcel
Parcelable:一個規范接口,定義實現Parcelable需要實現的功能
Parcel:數據存取的工具類,供用戶在實現Parcelable接口時存取自定義數據用,也供系統傳遞數據時使用。
Parcelable實現要點:
1)writeToParcel 方法。該方法將類的數據寫入外部提供的Parcel中。聲明如下: writeToParcel (Parcel dest, int flags) 。
2)describeContents方法。
3)靜態的Parcelable.Creator接口,本接口有兩個方法:
createFromParcel(Parcel in) 實現從in中創建出類的實例的功能
newArray(int size) 創建一個類型為T,長度為size的數組。
備注:
android 中自定義的對象序列化的問題有兩個選擇一個是Parcelable,另外一個是Serializable。
一 序列化原因:
1、永久性保存對象,保存對象的字節序列到本地文件中;
2、通過序列化對象在網絡中傳遞對象;
3、通過序列化在進程間傳遞對象。
二 至於選取哪種可參考下面的原則:
1、在使用內存的時候,Parcelable類比Serializable性能高,所以推薦使用Parcelable類。
2、Serializable在序列化的時候會產生大量的臨時變量,從而引起頻繁的GC。
3、Parcelable不能使用在要將數據存儲在磁盤上的情況,因為Parcelable不能很好的保證數據的持續性在外界有變化的情況下。盡管Serializable效率低點,也不提倡用,但在這種情況下,還是建議你用Serializable 。
實現:
1、Serializable 的實現,只需要繼承 implements Serializable 即可。這只是給對象打了一個標記,系統會自動將其序列化。
2、Parcelabel 的實現,需要在類中添加一個靜態成員變量 CREATOR,這個變量需要繼承 Parcelable.Creator接口。