当前位置:操作系统 > 安卓/Android >>

Android 程式开发:(二)使用意图 —— 2.8 添加Category

通过使用Intent-Filter中的<category>元素,我们可以把activities进行分组。假设已经在AndroidManifest.xml中添加了<category>元素:<?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" /> 
                <!-- 注意这句代码-->  www.zzzyk.com
                <category android:name="net.learn2develop.Apps" /> 
 
                <data android:scheme="http" /> 
            </intent-filter> 
        </activity> 
    </application> 
 
</manifest> 
在这种情况下,以下的代码将直接调用MyBrowserActivity: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...")); 
在以上的代码中,我们使用addCategory()方法为Intent对象添加Category属性。如果遗漏了addCategory()这句,之前的代码仍然能够调用MyBrowserActivity,因为它仍然匹配了默认的Category:android.intent.category.DEFAULT。
 
但是,如果指定了一个并没有在Intent-Filter中定义的Category,那么,将不会有Activity被调用:
 
 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中添加如下代码,之前的代码就可以运行了:
 
<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属性,举个例子:
 
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中。


摘自 manoel的专栏
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,