【Android 开发教程】添加Category

[java]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.manoel.Intents"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="14" />  
  8.   
  9.     <uses-permission android:name="android.permission.CALL_PHONE" />  
  10.     <uses-permission android:name="android.permission.INTERNET" />  
  11.   
  12.     <application  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name" >  
  15.         <activity  
  16.             android:name=".IntentsActivity"  
  17.             android:label="@string/app_name" >  
  18.             <intent-filter>  
  19.                 <action android:name="android.intent.action.MAIN" />  
  20.   
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             </intent-filter>  
  23.         </activity>  
  24.         <activity  
  25.             android:name=".MyBrowserActivity"  
  26.             android:label="@string/app_name" >  
  27.             <intent-filter>  
  28.                 <action android:name="android.intent.action.VIEW" />  
  29.                 <action android:name="net.learn2develop.MyBrowser" />  
  30.   
  31.                 <category android:name="android.intent.category.DEFAULT" />
  32.                 <category android:name="com.manoel.Apps" />  
  33.   
  34.                 <data android:scheme="http" />  
  35.             </intent-filter>  
  36.         </activity>  
  37.     </application>  
  38.   
  39. </manifest>  
在这种情况下,以下的代码将直接调用MyBrowserActivity:
[java]  view plain copy
  1. Intent i = new  
  2.         Intent(android.content.Intent.ACTION_VIEW,  
  3.             Uri.parse("http://www.amazon.com"));  
  4. nbsp;// 注意这句代码  
  5. i.addCategory("com.manoel.Apps");  
  6. startActivity(Intent.createChooser(i, "Open URL using..."));  
在以上的代码中,我们使用addCategory()方法为Intent对象添加Category属性。如果遗漏了addCategory()这句,之前的代码仍然能够调用MyBrowserActivity,因为它仍然匹配了默认的Category:android.intent.category.DEFAULT。

但是,如果指定了一个并没有在Intent-Filter中定义的Category,那么,将不会有Activity被调用:

[java]  view plain copy
  1.  Intent i = new  
  2.          Intent(android.content.Intent.ACTION_VIEW,  
  3.              Uri.parse("http://www.amazon.com"));  
  4.  // i.addCategory("net.learn2develop.Apps");  
  5.   
  6.  // 这个category不匹配Intent-Filter中的任何category  
  7.  i.addCategory("net.learn2develop.OtherApps");  
  8.  startActivity(Intent.createChooser(i, "Open URL using..."));  
net.learn2develop.OtherApps,不匹配Intent-Filter中的任何一个Category,所以,运行以上代码的时候,将会抛出一个运行时异常。

但是,如果在AndroidManifest.xml中添加如下代码,之前的代码就可以运行了:

[java]  view plain copy
  1. <activity android:name=".MyBrowserActivity"  
  2.           android:label="@string/app_name">  
  3.     <intent-filter>  
  4.         <action android:name="android.intent.action.VIEW" />  
  5.         <action android:name="net.learn2develop.MyBrowser" />  
  6.         <category android:name="android.intent.category.DEFAULT" />  
  7.         <category android:name="net.learn2develop.Apps" />  
  8.         <!--  添加这句代码 -->  
  9.         <category android:name="net.learn2develop.OtherApps" />  
  10.         <data android:scheme="http" />  
  11.     </intent-filter>  
  12. </activity>  

也可以为Intent对象添加多重Category属性,举个例子:

[java]  view plain copy
  1. Intent i = new  
  2.         Intent(android.content.Intent.ACTION_VIEW,  
  3.             Uri.parse("http://www.amazon.com"));  
  4. // 多重的Category  
  5. i.addCategory("com.manoel.Apps");  
  6. i.addCategory("com.manoel.OtherApps");  
  7. i.addCategory("com.manoel.SomeOtherApps");  
  8. startActivity(Intent.createChooser(i, "Open URL using..."));  
因为Intent-Filter中并没有定义net.learn2develop.SomeOtherApps这个Category,以上的代码将不会调用MyBrowserActivity。如果想要解决这个问题,那么,在目标Activity被调用之前,添加到Intent对象中的所有Category属性都必须全部地被定义在Intent-Filter中。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值