zoukankan      html  css  js  c++  java
  • android学习笔记29——Intent/IntentFilter

    Intent/IntentFilter

    Intent封装android应用程序需要启动某个组件的“意图”,Intent还是应用程序组件之间通信的重要媒介。

    EG:Activity之间需要交换数据时,使用封装的Bundle对象,Intent来携带Bundle对象。

    Intent可用于启动android应用中的Activity,Service、BroadCastReceiver.

    使用Intent启动不同组件的方法如下:

    Activity

    startActivity(Intent intent)

    startActivityForResult(Intent intent,int requestCode)

    Service

    ComponentName startService(Intent service)

    boolean bindService(Intent service,ServiceConnection conn,int flags)

    BroadCastReceiver

    sendBroadCast(Intent intent)

    sendBroadCast(Intent intent,String receiverPermission)

    sendOrderedBroadCast(Intent intent,String receiverPermission,BroadCastReceiver resultReceiver,Handler scheduler,int initialCode,String initialData,Bundle initialExtras)

    sendOrderedBroadCast(Intent intent,String receiverPermission)

    sendStickyBroadCast(Intent intent)

    sendStickyOrderedBroadCast(Intent intent,BroadCastReceiver resultReceiver,Handler scheduler,int initialCode,String initialData,Bundle initialExtras)

    Intent对象包含的属性:

      Component(用于明确指定需要启动的目标组件)、Action、Data、Category、Extra(用于携带数据)、Flag

    Component==>

    Intent的属性Component属性需要接受一个ComponentName对象,该对象包含如下几个构造器:

    ComponentName(String pkg,String cls) 创建pkg所在包下的cls类所对应的组件
    ComponentName(Context pkg,String cls) 创建pkg所对应包下的cls类对应的组件
    ComponentName(Context pkg,Class<?>cls) 春创建pkg所对应的包下的cls类所对应的组件

    启动方式如下:

    等价于==》

    注意:启动后,如果需要在被启动的Activity获取启动Activity的相关信息,可使用如下方式进行获取:

      ComponentName comp = getIntent.getComponent();

      comp.getPackageName();

      comp.getClassName();

    Action、Category、intent-filter

    Intent的Action、Category属性都是一个普通的字符串,Action代表Intent所要完成的一个“抽象”动作,Category则用于为Action增加额外的附加信息。

    通常Action、Category属性会结合使用。

    注意:
      一个Intent最多只能有一个Action属性,通过setAction(string str)进行设置;

      但一个Intent可以包含多个Category属性,通过addCategory(string str)添加属性。

    EG:
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_GET_CONTENT);// 设置Action属性
    intent.setType("vnd.android.cursor.item/phone");// 设置Intent的Type属性
    startActivityForResult(intent, PICK_CONTACT);// 启动Activity,并希望获取到该Activity的结果

    Action要完成的只是一个抽象动作,这个动作具体有那个组件来完成,Action这个字符串本身并不管。

    EG:android提供的标准Action:Intent.Action_VIEW,只是表示一个抽象的查看操作,但具体查看什么、启动那个Activity来查看,Intent.Action_VIEW并不知道——其取决于Activity的<intent-filter>配置,

      只要某个Activity的<intent-filter.../>配置中包含了该Action_VIEW,该Activity就可能被启动。

    <intent-filter.../>元素是AndroidMainfest.xml文件中<activity.../>元素的子元素,<activity.../>元素用于为应用程序配置Activity;<intent-filter.../>用于配置该Activity所能响应的Intent.

    <intent-filter.../>通常可包含如下子元素:

      1.0——N个<action.../>子元素

      2.0——N个<category.../>子元素

      3.0——N个<data.../>子元素

    注意:当<intent-filter>子元素里包含多个<action>子元素——表明该Activity能响应Action属性值为其中任何一个字符串的Intent.

       当程序创建Intent时,该Intent默认启动Category属性值为Intent.CATEGORY_DEFAULT(android.intent.category.DEFAULT)常量的组件.

    用法一:

    如上图,代码中并未指定目标Intent的Category属性,但该Intent默认已有一个值为android.intent.category.DEFAULT的Category属性值,因此

    被启动Activity对应的配置元素<intent-filter>中至少包括一个如下<category.../>子元素:

    用法二:

    代码设置:

    配置方式:

    指定Action、Category调用系统Activity

    android内部提供了大量标准Action、Category常量,其中常用的用于启动Activity的标准Action常量以及对应字符串如下所示:

    ACTION_MAIN android.intent.action.MAIN 应用程序入口
    ACTION_VIEW android.intent.action.VIEW 显示指定数据
    ACTION_ATTACH_DATA android.intent.action.ATTACH_DATA 指定某块数据被附加到其他地方
    ACTION_EDIT android.intent.action.EDIT 编辑指定数据
    ACTION_PICK android.intent.action.PICK 从列表中选择某项并返回所选的数据
    ACTION_CHOOSER android.intent.action.CHOOSER 显示一个Activity选择器
    ACTION_GET_CONTENT android.intent.action.GET_CONTENT 让用户选择数据,并返回所选数据
    ACTION_DIAL android.intent.action.DIAL 显示拨号界面
    ACTION_CALL android.intent.action.CALL 直接向其他用户打电话
    ACTION_SEND android.intent.action.SEND 向其他人发送数据
    ACTION_SENDTO android.intent.action.SENDTO 向其他人发送消息
    ACTION_ANSWER android.intent.action.ANSWER
    应答电话
    ACTION_INSERT

    android.intent.action.INSERT

    插入数据
    ACTION_DELETE android.intent.action.DELETE 删除数据
    ACTION_RUN android.intent.action.RUN 运行数据
    ACTION_SYNC android.intent.action.SYNC 执行数据同步
    ACTION_PICK_ACTIVITY android.intent.action.PICK_ACTIVITY 用于选择Activity
    ACTION_SEARCH android.intent.action.SEARCH 执行搜索
    ACTION_WEB_SEARCH android.intent.action.WEB_SEARCH 执行Web搜索
    ACTION_FACTORY_TEST android.intent.action.FACTORY_TEST 工厂测试的入口点

    标准Category常用的常量以及对应的字符串如下所示:

    CATEGORY_DEFAULT android.intent.category.DEFAULT 默认的Category
    CATEGORY_BROWSABLE android.intent.category.BROWSABLE 指定该Activity能被浏览器安全调用
    CATEGORY_TAB android.intent.category.TAB 指定Activity作为TabActivity的Tab页
    CATEGORY_LAUNCHER android.intent.category.LAUNCHER Activity显示顶级程序列表中
    CATEGORY_INFO android.intent.category.INFO 用于提供包信息
    CATEGORY_HOME android.intent.category.HOME 设置该Activity随系统启动而运行
    CATEGORY_PREFERENCE android.intent.category.PREFERENCE 该Activity是参数面板
    CATEGORY_TEST android.intent.category.TEST 该Activity是一个测试
    CATEGORY_CAR-DOCK android.intent.category.CAR-DOCK 指定手机被插入汽车底座(硬件)时运行该Activity
    CATEGORY_DESK_DOCK android.intent.category.DESK_DOCK 指定手机被插入桌面底座(硬件)时运行该Activity
    CATEGORY_CAR_MODE android.intent.category.CAR_MODE 设置该Activity可在车载环境下使用

    Data、Type属性与intent-filter配置

    Data属性通用用于向Action属性提供操作的数据,Data属性接受一个Uri对象,一个Uri对象通常通过如下形式的字符串表示:

    content://com.android.contacts/contacts/1

    tel:123

    content、tel表示的是数据类型,而其后信息表示数据内容。

    一个合法的Uri对象即可决定操作那种类型的数据,也可指定具体的数据内容。

    Type属性,这用于明确指定Data数据的类型或MIME类型,Eg:指定“cnd.android.cursor.item/phone”字符串。

    通常来说,Intent不指定Data属性时Type属性才会其作用,否则android系统会根据Data属性值来分析数据类型,因此无须指定Type属性。

    实际上Data属性值可分为两个部分:数据类型和数据部分。

    Eg:

    content://com.android.contacts/contacts/1

    1.content:前缀:该前缀表明数据类型为联系人信息

    2.//com.android.contacts/contacts/1:该数据表明操作_id为1的联系人数据

     一旦Intent同时指定了Action、Data属性,android将可根据指定的数据类型来启动特定的应用程序,并对指定数据执行响应的操作。

    下面是几个Action属性、Data属性的组合:

    ACTION_VIEW content://com.android.contacts/contacts/1: 显示标识为1的联系人信息
    ACTION_EDIT content://com.android.contacts/contacts/1: 编辑标识为1的联系人信息
    ACTION_DIAL content://com.android.contacts/contacts/1: 显示向标识为1的联系人的拨号界面
    ACTION_VIEW tel:123: 显示向指定号码123拨号的界面
    ACTION_DIAL te:123: 显示向指定号码123拨号的界面
    ACTION_VIEW content://contacts/people/: 显示所有联系人列表的信息
    ..... .....

    通过这种组合方式非常方便的可以查看系统联系人信息。

    Eg:

    Extra属性

    Extra属性通用用于多个Action之间进行数据交换,Intent的Extra属性值应该是一个Bundle对象,Bundle对象就像一个Map对象,其可存入多组key-value对,

    这样可以通过Intent在不太Activity之间进行数据交换。

    使用Intent创建Tab页面  

    TabActivity,可参考:http://www.cnblogs.com/YYkun/p/5761041.html

     

     

  • 相关阅读:
    android 开机启动
    android 禁用home键盘
    android 获取各种窗体高度
    android 横竖屏切换
    android 还原短信
    android dp和px之间转换
    android BitMap、Drawable、inputStream及byte[] 互转
    手机卫士项目
    Android01_Android入门
    Android02_Activity
  • 原文地址:https://www.cnblogs.com/YYkun/p/5825859.html
Copyright © 2011-2022 走看看