zoukankan      html  css  js  c++  java
  • 指定Action、Category调用系统Activity

    1、Intent对象详解

    Android的应用程序包含三种重要组件:Activity、Service、BroadcastReceiver,应用程序采用一致的方式来启动它们----都是依靠Intent来进行启动的,Intent就封装了程序想要启动程序的意图,不仅如此,Intent还用于与被启动组件进行交换信息。

    组件类型

    启动方法

    Activity

    startActivity(Intent intent)

    startActivityForResult(Intent intent,intrequestCode)

    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)

    sendStickyOrderedBroadcast(Intent intent,BroadcastReceiver resultReceiver,Handler scheduler,int initialCode,String initialData,Bundle initialExtras)

    Intent对象大致包含Component、Action、Category、Data、Type、Extra和Flag这7种属性,其中Component用于明确指定需要启动的目标组件,而Extra则用于“携带”需要交换的数据。

    2、Intent的属性及Intent-filter配置

    (1)Component属性

    Intent的Component属性需要接受一个ComponentName对象,应用程序可根据给定的组件类去启动特定的组件。

    当程序通过Intent的Component属性(明确指定了启动哪个组件)启动特定组件时,被启动组件几乎不需要使用<intent-filter.../>元素进行配置。

    (2)Action、Category属性与intent-filter配置

    Intent的Action和Category属性都是一个普通的字符串,其中Action代表该Intent所要完成的一个抽象“动作”,Category则用于为Action增加额外的附加类别信息,通常,Action与Category结合使用。

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

    a、0~N个<action.../>子元素

    b、0~N个<category.../>子元素

    c、0~1个<data.../>子元素

    <action.../><category.../>子元素的配置非常简单,它们都可指定android:name属性,该属性的值就是一个普通字符串。

    当<activity.../>元素里的<intent-filter.../>子元素里包含多个<action.../>子元素(相当于指定了多个字符串)时,就表明该Activity能响应Action属性值为其中任意一个字符串的Intent。

    一个Intent对象最多只能包括一个Action属性,程序调用Intent的setAction(String str)方法来设置Action属性值;但一个Intent对象可包含多个Category属性,调用Intent的addCategory(String str)方法添加。

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

    (3)指定Action、Category调用系统Activity

    实际上,Android内部提供了大量标准Action、Category常量,其中用于启动Activity的标准Action常量及对应的字符串如下:

     

    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常量

    对应字符串

    简单说明

    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可在车载环境下使用

    3、Data、Type属性与intent-filter配置

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

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

    tel:123

    上面两个字符串的冒号前面大致指定了数据的类型,冒号后面是数据部分。

    Type属性则用于明确指定Data属性所指定数据的类型或MIME类型,当Intent不指定Data属性时Type属性才会起作用,否则Android系统将会根据Data属性值来分析数据的类型,因此无需指定Type属性。

    一旦为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     tel:123:显示向指定号码123拨号的界面

    ACTION_VIEW    content://contacts/people/:显示所有联系人列表的信息,通过这种组合可以方便地查看系统联系人

    4、Extra属性

    Intent的Extra属性通常用于在多个Action之间进行数据交换,Intent的Extra属性值应该是一个Bundle对象,Bundle对象就像一个Map对象,它可以存入多组key-value对,这样可以就可以通过Intent在不同Activity之间进行数据交换了。

  • 相关阅读:
    loaded the "*****" nib but the view outlet was not set 错误的解决办法。
    IBOutlet和IBAction
    initWithNibName 和 loadNibNamed 的区别
    iOS 应用是如何创建的
    Objective C中NULL、Nil、nil、NSNull 的区别
    Objective C数组的内存管理
    XCode 调试1
    META httpequiv 大全
    基于GoogleMap,Mapabc,51ditu,VirtualEarth,YahooMap Api接口的Jquery插件的通用实现(含源代码下载) 转
    SELECT 語法中,如何動態組合查詢條件(转)
  • 原文地址:https://www.cnblogs.com/daishuguang/p/3872803.html
Copyright © 2011-2022 走看看