Intent中的四个重要属性——Action、Data、Category、Extras

                           本文来自刘兆贤_Java高级,Android旅行,Java底层探索-CSDN博客 ,引用请注明出处!All Rights Reserved !

        Intent作为联系各Activity之间的纽带,除了常见的Activity之间的跳转以及数据传递,今天我们讲一讲它的一些特殊属性,可以作为深入研究Intent的一篇博客来看。

        关于常见Intent的使用,请参见:http://blog.csdn.net/reboot123/article/details/7018331

        复杂条件下的对于Intent,主要通过Action、Category和Data来过滤,找到符合条件的Activity。

   Intent主要有以下四个重要属性,它们分别为:

    Action:Action属性的值为Intent的一个字符串,它代表了系统中已经定义了一系列常用的动作。通过setAction()或在清单文件AndroidManifest.xml中设置。

示例:

				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_DIAL);
				startActivity(intent);
意思是要打开一个拨号界面,结果:

或者:

				Intent intent = new Intent();
				intent.setAction("android.intent.action.TargetAction");
				startActivity(intent);
        <activity
            android:name="com.example.TargetActivity"
            android:launchMode="singleTask" >
            <intent-filter>
                <action android:name="android.intent.action.TargetAction" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
意思是要打开一个过滤Action注册名为“android.intent.action.TargetAction”的Activity,结果:


则到达TargetActivity界面,这时action和category都要进行设置。此时Action就如广播一样,只负责发出事件,至于谁来响应,系统会找出合适的对象来反应,同样的都可以有N个组件来响应,不同的是Action的响应组件如果没有设置默认响应组件,则需要选择,如:

这就是多个应用响应一个Action的结果。

响应代码:

				intent.setAction(Intent.ACTION_SEND);
				intent.setType("message/rfc822");
				startActivity(intent);
有时Action也需要与Type合作才能选出合适的应用来。

    2、Data:Data通常是URI格式定义的操作数据。例如:tel:// 。通过setData()方法设置。

示例:

				Uri uri = Uri.parse("tel:13888888888");
				intent.setAction(Intent.ACTION_VIEW);
				intent.setData(uri);
				startActivity(intent);

效果图:

不仅打开了拨号界面,而且还把号码传进来,当然如果Uri写的是网址则会打开浏览器,写的坐标则打开地图,也都会把数据传进来。

示例:

				Intent intent = new Intent();
				Uri uri = Uri.fromFile(getFilesDir());
				intent.setDataAndType(uri, "application/pdf");
				startActivity(intent);
寻找能打开pdf文件的Activity,结果给了这么几种选择:


用于打开pdf文件,同样也可以设置图片格式、Word格式等。

 

         3、Category:Category属性用于指定当前动作(Action)被执行的环境。通过addCategory()方法或在清单文件AndroidManifest.xml中设置。默认为:CATEGORY_DEFAULT。

示例:

				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_MAIN);
				intent.addCategory(Intent.CATEGORY_LAUNCHER);
				startActivity(intent);
意思是要打开一个category注册为Intent.CATEGORY_LAUNCHER的Activity,结果:

这些应用有注册此Category。也可以直接回到桌面

示例:

				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_MAIN);
				intent.addCategory(Intent.CATEGORY_HOME);
				startActivity(intent);
 

  4、 Extras:Extras属性主要用于传递目标组件所需要的额外的数据。通过putExtras()方法设置。 Extras:Extras属性主要用于传递目标组件所需要的额外的数据。通过putExtras()方法设置。

  通常我们使用Intent来直接传递Bundle对象,但也可以传递其他系统内置的一些参数。

上面只是介绍了一些Intent的基础的用法,实际情况要根据需要,将Action、Category、Data进行组合使用,使用代码或者在配置文件完成。

附部分Intent属性值:

  Action:

      ACTION_MAIN:Android Application的入口,每个Android应用必须且只能包含一个此类型的Action声明。 

    ACTION_VIEW:系统根据不同的Data类型,通过已注册的对应Application显示数据。

    ACTION_EDIT:系统根据不同的Data类型,通过已注册的对应Application编辑示数据。 

    ACTION_DIAL:系统默打开拨号程序,如果Data中设置电话号码,则拨号框中会显示此号码。 

    ACTION_CALL:直接呼叫Data中所带的号码。 

    ACTION_ANSWER:接听来电。 

    ACTION_SEND:由用户指定发送方式进行数据发送操作。

    ACTION_SENDTO:系统根据不同的Data类型,通过已注册的对应Application进行数据发送操作。 

    ACTION_BOOT_COMPLETED:Android系统在启动完毕后发出带有此Action的广播(Broadcast)。 

    ACTION_TIME_CHANGED:Android系统的时间发生改变后发出带有此Action的广播(Broadcast)。 

    ACTION_PACKAGE_ADDED:Android系统安装了新的App之后发出带有此Action的广播(Broadcast)。 

    ACTION_PACKAGE_CHANGED:Android系统中已存在的App发生改变之后(如更新)发出带有此Action的广播(Broadcast)。 

    ACTION_PACKAGE_REMOVED:Android系统卸载App之后发出带有此Action的广播(Broadcast)。  

  Category:

      CATEGORY_DEFAULT:Android系统中默认的执行方式,按照普通Activity的执行方式执行。 

    CATEGORY_HOME:设置该组件为Home Activity。

    CATEGORY_PREFERENCE:设置该组件为Preference。 

    CATEGORY_LAUNCHER:设置为当前应用程序优先级最高的Activity,通常与ACTION_MAIN配合使用。 

    CATEGORY_BROWSABLE:设置该组件可以使用浏览器启动。 

    CATEGORY_GADGET:设置该组件可以内嵌到另外的Activity中。

  Extras:

      EXTRA_BCC:存放邮件密送人地址的字符串数组。 

    EXTRA_CC:存放邮件抄送人地址的字符串数组。

    EXTRA_EMAIL:存放邮件地址的字符串数组。 

    EXTRA_SUBJECT:存放邮件主题字符串。 

    EXTRA_TEXT:存放邮件内容。 

    EXTRA_KEY_EVENT:以KeyEvent对象方式存放触发Intent的按键。  

    EXTRA_PHONE_NUMBER:存放调用ACTION_CALL时的电话号码。 

  

   Data:

      tel://:号码数据格式,后跟电话号码。 

    mailto://:邮件数据格式,后跟邮件收件人地址。

    smsto://:短息数据格式,后跟短信接收号码。

    content://:内容数据格式,后跟需要读取的内容。 

    file://:文件数据格式,后跟文件路径。

    market://search?q=pname:pkgname:市场数据格式,在Google Market里搜索包名为pkgname的应用。

    geo://latitude, longitude:经纬数据格式,在地图上显示经纬度所指定的位置。

MimeType:

 {".3gp",    "video/3gpp"}, 

            {".apk",    "application/vnd.android.package-archive"}, 
            {".asf",    "video/x-ms-asf"}, 
            {".avi",    "video/x-msvideo"}, 
            {".bin",    "application/octet-stream"}, 
            {".bmp",    "image/bmp"}, 
            {".c",  "text/plain"}, 
            {".class",  "application/octet-stream"}, 
            {".conf",   "text/plain"}, 
            {".cpp",    "text/plain"}, 
            {".doc",    "application/msword"}, 
            {".docx",   "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, 
            {".xls",    "application/vnd.ms-excel"},  
            {".xlsx",   "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, 
            {".exe",    "application/octet-stream"}, 
            {".gif",    "image/gif"}, 
            {".gtar",   "application/x-gtar"}, 
            {".gz", "application/x-gzip"}, 
            {".h",  "text/plain"}, 
            {".htm",    "text/html"}, 
            {".html",   "text/html"}, 
            {".jar",    "application/java-archive"}, 
            {".java",   "text/plain"}, 
            {".jpeg",   "image/jpeg"}, 
            {".jpg",    "image/jpeg"}, 
            {".js", "application/x-javascript"}, 
            {".log",    "text/plain"}, 
            {".m3u",    "audio/x-mpegurl"}, 
            {".m4a",    "audio/mp4a-latm"}, 
            {".m4b",    "audio/mp4a-latm"}, 
            {".m4p",    "audio/mp4a-latm"}, 
            {".m4u",    "video/vnd.mpegurl"}, 
            {".m4v",    "video/x-m4v"},  
            {".mov",    "video/quicktime"}, 
            {".mp2",    "audio/x-mpeg"}, 
            {".mp3",    "audio/x-mpeg"}, 
            {".mp4",    "video/mp4"}, 
            {".mpc",    "application/vnd.mpohun.certificate"},        
            {".mpe",    "video/mpeg"},   
            {".mpeg",   "video/mpeg"},   
            {".mpg",    "video/mpeg"},   
            {".mpg4",   "video/mp4"},    
            {".mpga",   "audio/mpeg"}, 
            {".msg",    "application/vnd.ms-outlook"}, 
            {".ogg",    "audio/ogg"}, 
            {".pdf",    "application/pdf"}, 
            {".png",    "image/png"}, 
            {".pps",    "application/vnd.ms-powerpoint"}, 
            {".ppt",    "application/vnd.ms-powerpoint"}, 
            {".pptx",   "application/vnd.openxmlformats-officedocument.presentationml.presentation"}, 
            {".prop",   "text/plain"}, 
            {".rc", "text/plain"}, 
            {".rmvb",   "audio/x-pn-realaudio"}, 
            {".rtf",    "application/rtf"}, 
            {".sh", "text/plain"}, 
            {".tar",    "application/x-tar"},    
            {".tgz",    "application/x-compressed"},  
            {".txt",    "text/plain"}, 
            {".wav",    "audio/x-wav"}, 
            {".wma",    "audio/x-ms-wma"}, 
            {".wmv",    "audio/x-ms-wmv"}, 
            {".wps",    "application/vnd.ms-works"}, 
            {".xml",    "text/plain"}, 
            {".z",  "application/x-compress"}, 
            {".zip",    "application/x-zip-compressed"}, 
 

参考: http://www.cnblogs.com/wisekingokok/archive/2011/08/22/2149847.html
以下是一些摘抄,加深大家对隐式启动方式的理解!

1.在<intent-filter></intent-filter>里可以有多个

<data android:scheme="xxxx" android:host="yyyy" android:port="uuu"/>,只需匹配其中一个即可。

语法:

<intent-filter>

<data android:host="string"

       android:mimeType="string"

       android:path="string"

       android:pathPattern="string"

       android:pathPrefix="string"

       android:port="string"

       android:scheme="string" />

</intent-filter>

也可以分开写,如:

       <data android:scheme="something" android:host="project.example.com" android:port="80"/>

 等同于这样写:

 <data android:scheme="something"/>

 <data android:host="project.example.com"/>

 <data android:port="80"/>

等同于Uri uri = Uri.parse("something://project.example.com:80");

Uri的格式:scheme://host:port/path or pathPrefix or pathPattern

如果scheme没有指定,那其它的属性均无效;

如果host没有指定,那么port,path,pathPrefix,pathPattern均无效;

如果在manifest里这样写:<data android:scheme="something" android:host="project.example.com" />

那么Uri uri = Uri.parse("something://project.example.com"); 才可以匹配

2.在<intent-filter>里可以有多个data或action ,只需匹配其中一个即可。

3.当匹配不上任何Activity的话,会发生异常,跳出对话框:很抱歉...某某应用程序意外停止,请重试。

4.上面所说的全部适用于Service和BroadcastReceiver。

摘抄来自于:如流,新一代智能工作平台

感谢原作者,带领我们对intent-filter有更详细的理解,向他致敬!
实际上打开应用组件不仅仅可以使用Intent,而且还可以使用路由的方式,通过依赖注入,反射获取intent,来打开模块化后的其他项目中的某个页面。详见: Android路由实现-CSDN博客

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘兆贤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值