Getting started with Android (19th): Use Intent to call the "built-in" app Category to add the Category attribute

Source: Internet
Author: User

By using the <category> element in Intent-Filter, activities can be grouped. Assume that the <category> element has been added to AndroidManifest. xml:


[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" type = "codeph" text = "/codeph">
 
<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"/>
<! -- Pay attention to this code -->
<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" type = "codeph" text = "/codeph">

<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"/>
<! -- Pay attention to this code -->
<Category android: name = "net. learn2develop. Apps"/>

<Data android: scheme = "http"/>
</Intent-filter>
</Activity>
</Application>

</Manifest> in this case, the following code directly calls MyBrowserActivity:


[Java] Intent I = new
Intent (android. content. Intent. ACTION_VIEW,
Uri. parse ("http://www.amazon.com "));
Nbsp; // pay attention to this Code
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 "));
// Pay attention to this Code
I. addCategory ("net. learn2develop. Apps ");
StartActivity (Intent. createChooser (I, "Open URL using ..."));
In the above Code, we use the addCategory () method to add the Category attribute to the Intent object. If addCategory () is omitted, the previous code can still call 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, no Activity will be called:


[Java] 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 category in the Intent-Filter.
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 ");

// This category does not match any category in the Intent-Filter.
I. addCategory ("net. learn2develop. OtherApps ");
StartActivity (Intent. createChooser (I, "Open URL using... "); net. learn2develop. otherApps does not match any Category in the Intent-Filter. Therefore, when you run the above Code, a runtime exception will be thrown.

However, if the following code is added to AndroidManifest. xml, the previous code can run:


[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"/>
<! -- Add the code -->
<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"/>
<! -- Add the code -->
<Category android: name = "net. learn2develop. OtherApps"/>
<Data android: scheme = "http"/>
</Intent-filter>
</Activity>
You can also add multiple Category attributes to an Intent object. For example:


[Java] Intent I = new
Intent (android. content. Intent. ACTION_VIEW,
Uri. parse ("http://www.amazon.com "));
// Multiple 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 "));
// Multiple Category
I. addCategory ("net. learn2develop. Apps ");
I. addCategory ("net. learn2develop. OtherApps ");
I. addCategory ("net. learn2develop. SomeOtherApps ");
StartActivity (Intent. createChooser (I, "Open URL using... "); because the Intent-Filter does not define net. learn2develop. someOtherApps: the Category. The above code will not call MyBrowserActivity. To solve this problem, all the Category attributes added to the Intent object must be defined in Intent-Filter before the target Activity is called.
 

From the column of horsttnann
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.