intent是具有其相關(guān)聯(lián)數(shù)據(jù)的動作。
Android使用Intents調(diào)用組件。Android中的組件包括
你可以使用intent來調(diào)用外部應(yīng)用程序或內(nèi)部組件。
你可以使用intent來引發(fā)事件,使別人可以以類似于發(fā)布-訂閱模型的方式進(jìn)行響應(yīng)。
你可以使用intent引發(fā)警報(bào)。
以下代碼顯示了如何使用Intent打開一個(gè)Activity。
假設(shè)你進(jìn)行了以下活動:
public class BasicViewActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... } }
然后,你在清單文件中注冊這個(gè)activity,使其可供其他應(yīng)用程序調(diào)用。
<activity android:name=".BasicViewActivity" android:label="Basic View Tests"> <intent-filter> <action android:name="cn.w3cschool.intent.action.ShowBasicView"/> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
要使用intent調(diào)用此BasicViewActivity:
public static void invokeMyApplication(Activity parentActivity) { String actionName= "cn.w3cschool.intent.action.ShowBasicView"; Intent intent = new Intent(actionName); parentActivity.startActivity(intent); }
動作名稱的一般約定是
<your-package-name>.intent.action.YOUR_ACTION_NAME
BasicViewActivity可以獲取調(diào)用它的intent。
class BasicViewActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... ... Intent intent = this.getIntent(); if (intent == null) { Log.d("test tag", "This activity is invoked without an intent"); } } }
intent有四個(gè)部分:
在Android中,intent通常是成對的:動作和數(shù)據(jù)。
該動作描述了要執(zhí)行什么,例如編輯項(xiàng)目,查看項(xiàng)目的內(nèi)容等等。
數(shù)據(jù)指定受影響的內(nèi)容,例如聯(lián)系人數(shù)據(jù)庫中的人員。
數(shù)據(jù)被指定為Uri對象。
一些行動的例子如下:
數(shù)據(jù)的一些示例包括以下:
動作和數(shù)據(jù)對描述了要執(zhí)行的操作。
例如,要撥打電話號碼,你將使用對 ACTION_DIAL/tel:+999234567
。
要顯示存儲在手機(jī)中的聯(lián)系人列表,請使用對ACTION_VIEW/content://contacts
。
要從聯(lián)系人列表中選擇聯(lián)系人,請使用對ACTION_PICK/content://contacts
。
你可以使用intent過濾器中的< category>
元素將activity分組。
AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.w3cschool.Intents" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" /> <uses-permission android:name="android.permission.CALL_PHONE"/> <uses-permission android:name="android.permission.INTERNET"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".IntentsActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MyBrowserActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="cn.w3cschool.MyBrowser" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="cn.w3cschool.Apps" /> <data android:scheme="http" /> </intent-filter> </activity> </application> </manifest>
以下代碼將直接調(diào)用MyBrowerActivity:
Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse ("http://www.15014759268.cn")); i.addCategory("cn.w3cschool.Apps"); startActivity(Intent.createChooser(i, "Open URL using..."));
如果省略 addCategory()
語句,上述代碼仍然會調(diào)用MyBrowerActivity,因?yàn)樗匀黄ヅ淠J(rèn)類別 android.intent.category.DEFAULT
。
對于以下代碼,它不匹配在intent過濾器中定義的類別,因此不會啟動任何activity。
以下代碼引用類別cn.w3cschool.OtherApps,它不匹配intent過濾器中的任何類別,因此如果不使用intent類的createChoose()方法,將會引發(fā)運(yùn)行時(shí)異常。
Intent i = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse ("http://www.15014759268.cn")); //i.addCategory("cn.w3cschool.Apps"); //this category does not match any in the intent-filter i.addCategory("cn.w3cschool.OtherApps"); startActivity(Intent.createChooser(i, "Open URL using..."));
更多建議: