By using the <category> elements in Intent-filter, we can group the activities. Suppose you have added a <category> element to the Androidmanifest.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/"
Android "package=" Com.manoel.Intents "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "/> <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= "Andr" Oid.intent.category.LAUNCHER "/> </intent-filter> </activity> <a CtiVity android:name= ".
Mybrowseractivity "android:label=" @string/app_name "> <intent-filter> <action android:name= "Android.intent.action.VIEW"/> <action android:name= "Net.learn2de" Velop.
Mybrowser "/> <category android:name=" Android.intent.category.DEFAULT "/>"
<category android:name= "Com.manoel.Apps"/> <data android:scheme= "http"/> </intent-filter> </activity> </application> </manifes T>
In this case, the following code calls the Mybrowseractivity directly:
Intent i = new
Intent (Android.content.Intent.ACTION_VIEW,
uri.parse ("http://www.amazon.com"));
nbsp;//Note that the code
i.addcategory ("Com.manoel.Apps");
StartActivity (Intent.createchooser (i, "Open URL using ..."));
In the above code, we use the Addcategory () method to add the Category property to the intent object. If Addcategory () is omitted, the previous code can still invoke mybrowseractivity because it still matches the default Category:android.intent.category.DEFAULT.
However, if you specify a category that is not defined in Intent-filter, there will be no activity being invoked:
Intent i = new
Intent (Android.content.Intent.ACTION_VIEW,
uri.parse ("http://www.amazon.com"));
I.addcategory ("Net.learn2develop.Apps");
This category does not match any of the category
I.addcategory in Intent-filter ("Net.learn2develop.OtherApps");
StartActivity (Intent.createchooser (i, "Open URL
using ..."));
Net.learn2develop.OtherApps, does not match any of the category in Intent-filter, so when you run the above code, a Run-time exception is thrown.
However, if you add the following code to the Androidmanifest.xml, the previous code can run:
<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 "/>
<!-- Add this code-->
<category android:name=" Net.learn2develop.OtherApps "/>
<data android:scheme=" http "/>
</intent-filter>
</activity>
You can also add multiple category properties to a intent object, for example:
Intent i =
new
Intent (Android.content.Intent.ACTION_VIEW,
uri.parse ("http://www.amazon.com"));
Multiple category
i.addcategory ("Com.manoel.Apps");
I.addcategory ("Com.manoel.OtherApps");
I.addcategory ("Com.manoel.SomeOtherApps");
StartActivity (Intent.createchooser (i, "Open URL using ..."));
Because this category is not defined in Intent-filter, the above code will not invoke mybrowseractivity. If you want to solve this problem, all category attributes added to the intent object must be fully defined in Intent-filter before the target activity is invoked.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/