很多人对文档中的Intent和IntentFilter不理解是什么意思,我这里举例解释下。 Intent字面意思就是目标,目的。通俗一点,需要达成某些目标,则需要提供一些动作,这些目标的分类,以及达成这些目标所需要的一些数据等等。Android中的Intent通过Action,Category和data等属性进行了相应的描述,我们想做某些事情(达成某些目标),就需要填写这些参数的部分或全部,这样Android才会帮助我们自动的去进行某些操作。 IntentFilter是配合Intent而生的,你有目标行动或者结果,那么那些行动和结果就会有他完成的特定要求,这些要求就是IntentFilter,可以理解为Intent和IntentFilter是相对应的。

<activity android:name=".TestService" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.STORE_REQUEST"></action>
                <category android:name="android.intent.category.ALTERNATIVE" />
                <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.TIGERTIAN"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
                <data android:scheme="x-id"></data>
            </intent-filter>
            
            <intent-filter>
                <action android:name="android.intent.action.EDIT"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
            <category android:name="android.intent.category.BROWSABLE"></category>
</intent-filter>
        </activity>

上面的Activity有三个Filter,第一个是给Android系统用的,表示这个Activity可以显示在桌面上(Launcher中)。同时Alternative表明,这个Activity可以 变成OptionMenu,供其他Activity直接调用。 后面两个Filter就是我自定义的了,第二个Filter可以在其他Activity中用如下方法直接调用:

      Uri uri = Uri.parse("x-id://www.google.com/getDetails?id=123");
                Intent in = new Intent();
                in.setAction("android.intent.action.TIGERTIAN");
                in.addCategory(Intent.CATEGORY_DEFAULT);
                in.setData(uri);
                //in.setClassName("com.tigertian.service", "com.tigertian.service.TestService");
                TestActivity.this.startActivity(in);

在Filter配置中CATEGORY_DEFAULT是不可缺少的,想调用这个Service,可以不指定Class,但其他条件必须匹配(CATEGORY_DEFAULT可以不设置,Android默认会自动加上),通过Action,category和data就可以调用相应的Activity了,这是Android帮你做的,当然如果系统中存在多个匹配这些条件的Activity或者Service,Android根据优先级进行调用。 第三个调用方式如下:

Uri uri = Uri.parse("x-id://www.google.com/getDetails?id=123");
                Intent in = new Intent();
                in.setAction(Intent.ACTION_EDIT);
                in.addCategory(Intent.CATEGORY_BROWSABLE);
                //in.setData(uri);
                //in.setComponent(new ComponentName("com.tigertian.service", "com.tigertian.service.TestService"));
                TestActivity.this.startActivity(in);

可以不指定CATEGORY_DEFAULT,Android自动帮你添加。就是这么简单。

Android的Intent和IntentFilter应用说明一例的更多相关文章

  1. [转]android笔记--Intent和IntentFilter详解

    Intent用于启动Activity, Service, 以及BroadcastReceiver三种组件, 同时还是组件之间通信的重要媒介. 使用Intent启动组件的优势1, Intent为组件的启 ...

  2. android笔记--Intent和IntentFilter详解

    本文转载自:https://www.cnblogs.com/liushengjie/archive/2012/08/30/2663066.html 本文转载自:https://www.cnblogs. ...

  3. android学习日记20--连接组件之Intent和IntentFilter

    上次刚了解完Android的四大组件,现在学习组件间通信的Intent和IntentFilter 一.Intent 1.简述 Intent(意图)在应用程序运行时连接两个不同组件,是一种运行时的绑定机 ...

  4. Android两个注意事项.深入了解Intent和IntentFilter(两)

    深入理解Intent和IntentFiler(二) 转载请表明出处:http://blog.csdn.net/u012637501(嵌入式_小J的天空)     在上一篇文章中,我们比較具体学习了&q ...

  5. Android应用程序组件之间的通信Intent和IntentFilter

    Android应用程序的基本组件,这些基本组建除了Content Provider之外,几乎全部都是依靠Intent对象来激活和通信的. 下面介绍Intent类,并通过例子来说明Intent一般用法 ...

  6. Android Intent和IntentFilter详解与使用及实现系统“分享”接口

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

  7. Intent和IntentFilter详解

    Intent用于启动Activity,Service, 以及BroadcastReceiver三种组件, 同时还是组件之间通信的重要媒介. 使用Intent启动组件的优势1, Intent为组件的启动 ...

  8. Android中Intent传递对象的两种方法(Serializable,Parcelable)

    今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...

  9. 略谈Android之Intent

    前言:大家都知道Android程序的实现一般都由四大组件构成: Activity :Android程序实现功能的主体,提供了和客户交互的界面,也提供了和后台交互的功能. Service :是一个没有界 ...

随机推荐

  1. 【C#】is 和 as

    看个例子: public class User { } public class Group { } class Program { static void Main(string[] args) { ...

  2. 系统架构一:snmp+mrtg服务器监控

    //@author:yuan<turing_zhy@163.com> 码字不易,转载请注明出处 #================================== 开始,服务器准备   ...

  3. ubuntu 14.04 x64下安装libreoffice

    LibreOffice是ubuntu 上的办公软件很多人都知道微软公司的的Word.Excel.PowerPoint和Outlook,但是很少有人知道LibreOffice. LibreOffice靠 ...

  4. 正确处理类的复合关系------新标准c++程序设计

    假设要编写一个小区养狗管理程序,该程序需要一个“主人”类,还需要一个“狗”类.狗是有主人的,主人也有狗.假定狗只有一个主人,但一个主人可以有最多10条狗.该如何处理“主人”类和“狗”类的关系呢?下面是 ...

  5. 加密模块(md5)

    一.md5加密 import hashlib s = ' print(s.encode()) m = hashlib.md5(s.encode())# 必须得传一个bytes类型的 print(m.h ...

  6. luoguP4234 最小差值生成树

    https://www.luogu.org/problemnew/show/P4234 按照边的权值从小到大排序,依次加入,并删除能够删除的权值最小的一条边,用 set 维护当前所有边的边权,并查集维 ...

  7. Linux--CentOS7使用firewalld打开关闭防火墙与端口

    1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...

  8. GN算法---《Community structure in social and biological networks》这篇论文讲了什么?

    用中文记下这篇论文的大致意思,以防止忘了.好记性不如烂笔头! 摘要:最近的一些研究在研究社交网络或WWW.研究者都集中于研究网络的“小世界性”,“幂率分布特性”,“网络传递性”(聚类性吧).本文提出网 ...

  9. TX2 Clone

    由于给TX2配置了很多的开发环境,也修改了一些驱动,想将这些环境能够完整的迁移到一块bare TX2,于是尝试了clone的方法. 这种方法的优点是: 确保了移植的TX2 与已经配置好的环境是一致的: ...

  10. _.each _.map _.filter javascript 语法

    1.forEach():没有返回值,只是针对每个元素调用func     简单来说,就是将数组中的每个值,依次进行遍历,执行功能函数     如果只需对数组元素进行操作,不需返回任何值时适用 var ...