intent是具有其相關聯(lián)數(shù)據(jù)的動作。
Android使用Intents調(diào)用組件。Android中的組件包括
你可以使用intent來調(diào)用外部應用程序或內(nèi)部組件。
你可以使用intent來引發(fā)事件,使別人可以以類似于發(fā)布-訂閱模型的方式進行響應。
你可以使用intent引發(fā)警報。
以下代碼顯示了如何使用Intent打開一個Activity。
假設你進行了以下活動:
public class BasicViewActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... } }
然后,你在清單文件中注冊這個activity,使其可供其他應用程序調(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有四個部分:
在Android中,intent通常是成對的:動作和數(shù)據(jù)。
該動作描述了要執(zhí)行什么,例如編輯項目,查看項目的內(nèi)容等等。
數(shù)據(jù)指定受影響的內(nèi)容,例如聯(lián)系人數(shù)據(jù)庫中的人員。
數(shù)據(jù)被指定為Uri對象。
一些行動的例子如下:
數(shù)據(jù)的一些示例包括以下:
動作和數(shù)據(jù)對描述了要執(zhí)行的操作。
例如,要撥打電話號碼,你將使用對 ACTION_DIAL/tel:+999234567
。
要顯示存儲在手機中的聯(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://m.o2fo.com")); i.addCategory("cn.w3cschool.Apps"); startActivity(Intent.createChooser(i, "Open URL using..."));
如果省略 addCategory()
語句,上述代碼仍然會調(diào)用MyBrowerActivity,因為它仍然匹配默認類別 android.intent.category.DEFAULT
。
對于以下代碼,它不匹配在intent過濾器中定義的類別,因此不會啟動任何activity。
以下代碼引用類別cn.w3cschool.OtherApps,它不匹配intent過濾器中的任何類別,因此如果不使用intent類的createChoose()方法,將會引發(fā)運行時異常。
Intent i = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse ("http://m.o2fo.com")); //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..."));
更多建議: