本章节翻译自《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中。

android category的更多相关文章

  1. android Intent介绍

    Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 ...

  2. android intent和intent action大全

    1.Intent的用法:(1)用Action跳转1,使用Action跳转,如果有一个程序的AndroidManifest.xml中的某一个 Activity的IntentFilter段中 定义了包含了 ...

  3. Android开发学习笔记:Intent的简介以及属性的详解【转】

    一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...

  4. Android 隐式意图的配置

    本文地址:http://www.cnblogs.com/wuyudong/p/5677473.html,转载请注明源地址. <Android 显示意图激活另外一个Actitity>一文介绍 ...

  5. Android开发面试经——2.常见Android基础笔试题

     标签: androidAndroid基础Android面试题Android笔试题 2015-03-12 15:04 3361人阅读 评论(3) 收藏 举报  分类: Android开发(29)  版 ...

  6. Android总结篇——Intent机制详解及示例总结

         最近在进行android开发过程中,在将 Intent传递给调用的组件并完成组件的调用时遇到点困难,并且之前对Intent的学习也是一知半解,最近特意为此拿出一些时间,对Intent部分进行 ...

  7. 【转载】Android开发学习笔记:Intent的简介以及属性的详解

    http://liangruijun.blog.51cto.com/3061169/634411/ 一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent ...

  8. Android开发学习之Intent具体解释

    Intent简单介绍和具体解释:           Intent:协助应用间的交互与通信,Intent负责相应用中一次操作的动作.动作涉及的数据.附加数据进行描写叙述.               ...

  9. Android采访开发——2.通用Android基础笔试题

    注意finddreams博客: http://blog.csdn.net/finddreams/article/details/44219231 正值跳槽的热季.整理一下Android面试中最常考的笔 ...

随机推荐

  1. masonry瀑布流的使用

    今天在使用masonry.pkgd.min.js瀑布流的时候遇到一个很奇怪的问题,官网显示正常,而我的就是显示不正确,然后我又查看一遍,原来要加这段代码就ok了,记录一下,怕以后还会遇到这个问题 *, ...

  2. [POST] What Is the Linux fstab File, and How Does It Work?

    If you’re running Linux, then it’s likely that you’ve needed to change some options for your file sy ...

  3. iOS 常用的几个math函数

    1.取整数 double ceil (double); 取上整 double floor (double); 取下整 2.绝对值 double fabs (double);求绝对值 double ca ...

  4. [Spring学习笔记 1 ] Spring 简介,初步知识--Ioc容器详解 基本原理。

    一.Spring Ioc容器详解(1) 20131105 1.一切都是Bean Bean可是一个字符串或者是数字,一般是一些业务组件. 粒度一般比较粗. 2.Bean的名称 xml配置文件中,id属性 ...

  5. jenkins和gitlab结合的时候出错

    Started by user zhaoliang Building in workspace /var/lib/jenkins/workspace/ZuoYeah_Static_Production ...

  6. TCP_NODELAY 和 TCP_NOPUSH的解释

    一.问题的来源 今天看到 huoding 大哥分享的 lamp 面试题,其中一点提到了: Nginx 有两个配置项: TCP_NODELAY 和 TCP_NOPUSH ,请说明它们的用途及注意事项. ...

  7. iphone 恢复出厂设置方法

    1.下载安装并打开itunes. 2.让手机进入恢复模式: 一.先长按住电源键,出现关机选项时,请滑动关机: 二.随后再按电源键开机,屏幕会出现苹果标志,不要松开电源键: 三.接着再按住主屏 Home ...

  8. k8s实战之数据卷(volume)

    一.概述 数据卷用于实现容器持久化数据,k8s对于数据卷重新定义,提供了丰富强大的功能:数据卷分为三类: 本地数据卷,网络数据卷和信息数据卷 二.

  9. kubernetes架构之二

    一.概述 IaaS:即基础设施即服务,通过虚拟化和分布式存储等技术,实现对包括服务器.存储设备.网络设备等各种物理资源的抽象:从而形成了一个可扩展.可按需分配的虚拟资源池.最具代表性的IaaS产品有A ...

  10. 【Spring】Spring之依赖注入(DI)传递参数的方式

    DI依赖注入传入参数的方式,这里介绍了基本数据类型,集合,符合数据类型的传递(String类型比较特殊,其传递值和基本数据类型的方法一致) 注入基本数据类型和String类型 通过setter方法注入 ...