Use Intent Categories
Description
You can group your activities into categories by
using the <category>
element in the intent filter.
Example
AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.java2s.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="com.java2s.MyBrowser" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.java2s.Apps" />
<data android:scheme="http" />
</intent-filter>
</activity>
</application>
</manifest>
The following code will directly invoke the MyBrowerActivity activity:
Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse ("http://www.java2s.com"));
i.addCategory("com.java2s.Apps");
startActivity(Intent.createChooser(i, "Open URL using..."));
If you omit the addCategory()
statement,
the preceding code will still invoke
the MyBrowerActivity activity
because it will still match the default category android.intent.category.DEFAULT
.
For the following code it does not match the category defined in the intent filter, therefore no activity will be launched:
Note
The following code references category com.java2s.OtherApps and it does not match any category in the intent filter, so a run-time exception will be raised if you don't use the createChoose() method of the Intent class.
Intent i = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse ("http://www.java2s.com"));
//i.addCategory("com.java2s.Apps");
//this category does not match any in the intent-filter
i.addCategory("com.java2s.OtherApps");
startActivity(Intent.createChooser(i, "Open URL using..."));