首页 > 其他 > 详细

使用Intent和IntentFilter进行通信

时间:2014-09-25 16:25:38      阅读:514      评论:0      收藏:0      [点我收藏+]

Intent的中文翻译就是“意图”。Android使用Intent来封装程序的调用意图。使用intent会提供统一的编程模型,而且能起到解耦合的作用。

使用Intent对象启动系统组件的方法:

startActivity(Intent intent)

startActivityForResult(Intent intent,int requestCode)

startService(Intent intent)

bindService(Intent intent,ServiceConnection conn,int flags)

sendBroadcast(Intent intent)

sendStickyBroadcast(Intent intent)等等

 

Intent对象常用属性

Intent对象由以下六个部分组成:

Component

Action

Data

Category

Extras

Flags

Intent寻找目标组件的两种方法

第一种:显式调用,通过Component属性直接指定;

指定了Component属性的Intent对象就已经明确了要启动哪一个组件,几乎不用在<intent-filter…>中配置,因此这种Intent也被成为显式Intent。

ComponentName com = new ComponentName("com.antroid.Test", "com.android.Test.TestActivity"); 
Intent  intent = new Intent(); 
intent.setComponent(com); 
startActivity(intent);
ComponentName comp =getIntent.getComponent();  //在目标页面查询ComponentName的信息
Log.v("msg",comp.getPackageName+"  "+comp.getClassName);

 

 

第二种:隐式调用,没有明确指定目标组件的名称,那么就要通过一定的条件过滤筛选。

我们通过在<intent-filter…/>中配置以下属性:

0~N个<action…/>

0~N个<category…/>

0~1个<data…/>

 

1、Action

Action是指Intent要完成的动作,是一个字符串常量。在Intent类里面定义了很多Action常量,其中有:

bubuko.com,布布扣

如intent.setAction(Intent.ACYION_CALL);

当然也可以自己定义Action常量,自定义的常量需要加上你的应用的包名作为前缀。

当包含多个Action元素时,就表示Activity能够响应Action属性值为其中任意一个字符串的Intent.

 

2、Category

Category也是一个字符串,提供了额外的信息。Intent类中也定义了一些Category常量:

bubuko.com,布布扣

 

需要注意的是:每个<intent-filter…/>组件中能包含多个<action…/>和<category…/>子元素。而每个Intent对象只能指定一个Action要求,但可以指定多个Category要求.只要组件满足的要求大于或等于Intent指定的要求,那么该Intent就能启动该组件。

如果某个Intent对象并未指定Category属性,则该Intent对象默认启动Category属性值为Intent.CATEGORY_DAFULT的组件。

 

3、Data

Data属性通常用于向Action属性提供操作的数据。Data属性接受一个URI对象。比如,Action是ACTION_EDIT时,数据域将是文档的URI;Action是ACTION_CALL时,数据域是 tel: URI ,带有要拨打的电话号码;如果Action是 ACTION_VIEW,则数据域是http: URI。

如·:http://www.baidu.com    tel:13800138000

 

4、Extra属性

用于在多个Action之间进行数据交换,可以携带Bundle对象

使用Intent和IntentFilter进行通信

原文:http://www.cnblogs.com/shinefy/p/3956553.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!