李sir_Blog

博客园 首页 联系 订阅 管理

本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/

 

通过使用Intent-Filter中的<category>元素,我们可以把activities进行分组。假设已经在AndroidManifest.xml中添加了<category>元素:

  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:

  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被调用:

  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中添加如下代码,之前的代码就可以运行了:

  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属性,举个例子:

  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中。

posted on 2015-11-06 10:09  李sir  阅读(272)  评论(0编辑  收藏  举报