使用广播接收器
2014-04-20 20:46
169 查看
之前 的用Intent 在不同的Activity之间跳转,可以说是直接 Intent,因为指定 了明确 的目标。
广播则是相当于间接Intent.
间接intent就是没有指定具体目标的Intent,只是在创建 是指定 了一些 信息。然后再广播出去,
所有 关心这种Intent的广播接收器都会接收并处理。
一.间接 Intent的说明信息
Action
data:要操作的数据 ,以uri的形式表示,
category:动作的附加 信息。
type :数据 的类型,一般由intent相关信息推导。
component:目标组件 ,一般由intent相关信息推导。
Extras :用来 传递参数 ,如发送邮件时,将邮件名,正文都加入到Extras中,再通过intent 发送给相关的activity。
发送广播 :
ContextWrapper.sendBroadcast( Intent intent )
二 .接收广播
使用广播接收器---BroadcastReceiver。每一个广播接收器都 要有一个Intent 过虑 器。
steps;
1. 新建 一个 Intent过虑 器IntentFilter。用
IntentFilter.IntentFilter (String action)
接下来再设定一些 属性,如
IntentFilter.addcategory(String category)。
2. 新建 一个广播接收器BroadcastReceiver ,并重写 onReceiver()方法。
BroadcastReceiver receiver = new BroadcastReceiver(){
public void onReceiver(Context ctx, Intent intent ){
//接收到广播 后执行的
}}
note: onReceiver()里面只能执行一些段时间的代码 ,超过5秒就会出去超时的对话框,用时多的可以放在一个线程里面,or Activity or Service里,再用Intent去启动它们。
3. 注册一个接收器
新建广播 接收器后并不能马上就工作,因为android系统 并不知道你已经 拥有了一个接收器,所以要注册。
ContextWrapper.registerReceiver (BroadcastReceiver receiver, IntentFilter filter )
4.当我们不再关注 广播 时,就要将接收器注销了
ContextWrapper.unregister (BroadcastReceiver receiver )
广播则是相当于间接Intent.
间接intent就是没有指定具体目标的Intent,只是在创建 是指定 了一些 信息。然后再广播出去,
所有 关心这种Intent的广播接收器都会接收并处理。
一.间接 Intent的说明信息
Action
data:要操作的数据 ,以uri的形式表示,
category:动作的附加 信息。
type :数据 的类型,一般由intent相关信息推导。
component:目标组件 ,一般由intent相关信息推导。
Extras :用来 传递参数 ,如发送邮件时,将邮件名,正文都加入到Extras中,再通过intent 发送给相关的activity。
发送广播 :
ContextWrapper.sendBroadcast( Intent intent )
二 .接收广播
使用广播接收器---BroadcastReceiver。每一个广播接收器都 要有一个Intent 过虑 器。
steps;
1. 新建 一个 Intent过虑 器IntentFilter。用
IntentFilter.IntentFilter (String action)
接下来再设定一些 属性,如
IntentFilter.addcategory(String category)。
2. 新建 一个广播接收器BroadcastReceiver ,并重写 onReceiver()方法。
BroadcastReceiver receiver = new BroadcastReceiver(){
public void onReceiver(Context ctx, Intent intent ){
//接收到广播 后执行的
}}
note: onReceiver()里面只能执行一些段时间的代码 ,超过5秒就会出去超时的对话框,用时多的可以放在一个线程里面,or Activity or Service里,再用Intent去启动它们。
3. 注册一个接收器
新建广播 接收器后并不能马上就工作,因为android系统 并不知道你已经 拥有了一个接收器,所以要注册。
ContextWrapper.registerReceiver (BroadcastReceiver receiver, IntentFilter filter )
4.当我们不再关注 广播 时,就要将接收器注销了
ContextWrapper.unregister (BroadcastReceiver receiver )
相关文章推荐
- 广播接收器的使用(1)
- [Android] 使用广播接收器Braodcast Receiver 监听手机来电 去电 挂断的事
- Android 中动态广播接收器和静态广播接收器的使用
- BPFP系列:优化电池使用时间:根据需要操作广播接收器
- 不同的activity使用bundle对象传值给广播接收器
- 广播接收器(BroadcastReceiver)的使用
- 不同的activity使用bundle对象传值给广播接收器
- 广播接收器的生命周期及使用
- 关于无法使用广播接收器对电话的监听问题
- Android的四大组件之一 BroadcastReceiver多种广播接收器的灵活使用
- Android 广播(Broadcast)及广播接收器(BroadcastReceiver)的使用
- 探索广播接收器的使用
- 安卓短信功能全解:调用系统短信功能发送短信、彩信,使用SmsManager发送短信,并监听发送短信的投递情况,使用广播接收器监听接收的短信。
- Android 优化电池使用时间——根据需要操作广播接收器
- 广播接收器的使用
- SP2010开发和VS2010专家"食谱"--第一章节--列表和事件接收器(6)--使用列表事件接收器
- Android 广播介绍以及使用示例
- Android开发之AlarmManager的使用(开启Activity、开启服务、开启广播)
- (转)Android - 活动(Activity), 服务(Service), 广播(Broadcast), 广播接收器(BroadcastReceiver)
- 浅谈 Android广播 BroadcastReceiver的使用