android category

时间:2023-12-23 14:01:26

本章节翻译自《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. <uses-sdk android:minSdkVersion="14" />
  7. <uses-permission android:name="android.permission.CALL_PHONE" />
  8. <uses-permission android:name="android.permission.INTERNET" />
  9. <application
  10. android:icon="@drawable/ic_launcher"
  11. android:label="@string/app_name" >
  12. <activity
  13. android:name=".IntentsActivity"
  14. android:label="@string/app_name" >
  15. <intent-filter>
  16. <action android:name="android.intent.action.MAIN" />
  17. <category android:name="android.intent.category.LAUNCHER" />
  18. </intent-filter>
  19. </activity>
  20. <activity
  21. android:name=".MyBrowserActivity"
  22. android:label="@string/app_name" >
  23. <intent-filter>
  24. <action android:name="android.intent.action.VIEW" />
  25. <action android:name="net.learn2develop.MyBrowser" />
  26. <category android:name="android.intent.category.DEFAULT" />
  27. <category android:name="com.manoel.Apps" />
  28. <data android:scheme="http" />
  29. </intent-filter>
  30. </activity>
  31. </application>
  32. </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. // 这个category不匹配Intent-Filter中的任何category
  6. i.addCategory("net.learn2develop.OtherApps");
  7. 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中。