Getting Started with Android Development (ii) Add category using Intent 2.8

Source: Internet
Author: User

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/

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.