通過使用Intent-Filter中的<category>元素,我們可以把activities進行分組。假設已經在AndroidManifest.xml中添加瞭<category>元素:
[java] <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.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:name=".IntentsActivity"
android:label="@string/app_name" >
<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="net.learn2develop.MyBrowser" />
<category android:name="android.intent.category.DEFAULT" />
<!– 註意這句代碼 –>
<category android:name="net.learn2develop.Apps" />
<data android:scheme="http" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.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:name=".IntentsActivity"
android:label="@string/app_name" >
<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="net.learn2develop.MyBrowser" />
<category android:name="android.intent.category.DEFAULT" />
<!– 註意這句代碼 –>
<category android:name="net.learn2develop.Apps" />
<data android:scheme="http" />
</intent-filter>
</activity>
</application>
</manifest>在這種情況下,以下的代碼將直接調用MyBrowserActivity:
[java] Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://www.amazon.com"));
nbsp;// 註意這句代碼
i.addCategory("net.learn2develop.Apps");
startActivity(Intent.createChooser(i, "Open URL using…"));
Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://www.amazon.com"));
// 註意這句代碼
i.addCategory("net.learn2develop.Apps");
startActivity(Intent.createChooser(i, "Open URL using…"));
在以上的代碼中,我們使用addCategory()方法為Intent對象添加Category屬性。如果遺漏瞭addCategory()這句,之前的代碼仍然能夠調用MyBrowserActivity,因為它仍然匹配瞭默認的Category:android.intent.category.DEFAULT。
但是,如果指定瞭一個並沒有在Intent-Filter中定義的Category,那麼,將不會有Activity被調用:
[java] Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://www.amazon.com"));
// i.addCategory("net.learn2develop.Apps");
// 這個category不匹配Intent-Filter中的任何category
i.addCategory("net.learn2develop.OtherApps");
startActivity(Intent.createChooser(i, "Open URL using…"));
Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://www.amazon.com"));
// i.addCategory("net.learn2develop.Apps");
// 這個category不匹配Intent-Filter中的任何category
i.addCategory("net.learn2develop.OtherApps");
startActivity(Intent.createChooser(i, "Open URL using…"));net.learn2develop.OtherApps,不匹配Intent-Filter中的任何一個Category,所以,運行以上代碼的時候,將會拋出一個運行時異常。
但是,如果在AndroidManifest.xml中添加如下代碼,之前的代碼就可以運行瞭:
[java] <activity android:name=".MyBrowserActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="net.learn2develop.MyBrowser" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="net.learn2develop.Apps" />
<!– 添加這句代碼 –>
<category android:name="net.learn2develop.OtherApps" />
<data android:scheme="http" />
</intent-filter>
</activity>
<activity android:name=".MyBrowserActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="net.learn2develop.MyBrowser" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="net.learn2develop.Apps" />
<!– 添加這句代碼 –>
<category android:name="net.learn2develop.OtherApps" />
<data android:scheme="http" />
</intent-filter>
</activity>
也可以為Intent對象添加多重Category屬性,舉個例子:
[java] Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://www.amazon.com"));
// 多重的Category
i.addCategory("net.learn2develop.Apps");
i.addCategory("net.learn2develop.OtherApps");
i.addCategory("net.learn2develop.SomeOtherApps");
startActivity(Intent.createChooser(i, "Open URL using…"));
Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://www.amazon.com"));
// 多重的Category
i.addCategory("net.learn2develop.Apps");
i.addCategory("net.learn2develop.OtherApps");
i.addCategory("net.learn2develop.SomeOtherApps");
startActivity(Intent.createChooser(i, "Open URL using…"));因為Intent-Filter中並沒有定義net.learn2develop.SomeOtherApps這個Category,以上的代碼將不會調用MyBrowserActivity。如果想要解決這個問題,那麼,在目標Activity被調用之前,添加到Intent對象中的所有Category屬性都必須全部地被定義在Intent-Filter中。
摘自 horsttnann的專欄