Android四大组件之一:BroadcastReceiver

一、BroadcastReceiver简介
BroadcastReceiver,用于异步接收广播Intent,广播Intent是通过调用Context.sendBroadcast()发送、BroadcastReceiver()接收。

广播Intent的发送是通过调用Context.sendBroadcast()、Context.sendOrderedBroadcast()、Context.sendStickyBroadcast()来实现的。通常一个广播Intent可以被订阅了此Intent的多个广播接收者所接收,广播接收者和JMS中的Topic消息接收者很相似。

广播是一种广泛运用的在应用程序之间传输信息的机制 。而 BroadcastReceiver 是对发送出来的广播进行过滤接收并响应的一类组件;

广播接收器只能接收广播,对广播的通知做出反应,很多广播都产生于系统代码,如:时区改变的通知、电池电量不足、用户改变了语言偏好,或者开机启动等
广播接收器没有用户界面,但是它可以为它们接收到信息启动一个Activity、使用NotificationManager来通知用户或者启动 Service 等等.


BroadcastReceiver 接收广播方式:

1. Normal broadcasts(正常广播),用 Context.sendBroadcast()发送是完全异步的,它们都运行在一个未定义的顺序,通常是在同一时间。这样会更有效,但意味着receiver不能包含所要使用的结果或中止的API。 
 
2. Ordered broadcasts(有序广播),用 Context.sendOrderedBroadcast()发送每次被发送到一个receiver。所谓有序,就是每个receiver执行后可以传播到下一个receiver,也可以完全中止传播——不传播给其他receiver。 而receiver运行的顺序可以通过matched intent-filter 里面的android:priority来控制,当priority优先级相同的时候,Receiver以任意的顺序运行。


二、BroadcastReceiver机制
在 Android 里面有各种各样的广播,比如电池的使用状态,电话的接收和短信的接收都会产生一个广播,应用程序开发者也可以监听这些广播并做出程序逻辑的处理。如图:



三、BroadcastReceiver生命周期

一个BroadcastReceiver 对象只有在被调用onReceive(Context, Intent)的才有效,当从该函数返回后,该对象就无效的了,结束生命周期。
因此从这个特征可以看出,在所调用的onReceive(Context, Intent)函数里,不能有过于耗时的操作,不能使用线程来执行。
生命周期只有十秒左右,如果在 onReceive() 内做超过十秒内的事情,就会报错 
如果有耗时的操作,应该在startService中来完成。因为当得到其他异步操作所返回的结果时,BroadcastReceiver 可能已经无效了。

四、BroadcastReceiver实现方式
step 1.自定义BroadcastReceiver类
继承BroadcastReceiver类,重写onReciver()方法
public class MusicReceiver extends BroadcastReceiver {      // receive Broadcast  
     
    @Override  
    public void onReceive(Context context, Intent intent) {  
         
        if(intent != null){  
            Bundle bundle = intent.getExtras();  
            Intent it = new Intent(context, MusicReceiverService.class);   
            it.putExtras(bundle);  
            if(bundle != null){  
                int op = bundle.getInt("op");  
                if(op == 4){  
                    context.stopService(it);        // stopService  
                }else{  
                    context.startService(it);       // startService  
                }  
            }  
        }  
    }  
}
step 2.注册广播
1.静态注册
AndroidManifest.xml中,application里面,定义receiver并设置要接收的action
<receiver android:name=".receiver.MusicReceiver" >  
    <intent-filter>  
        <action android:name="com.makk.receiver.musicReceiver" />  
    </intent-filter>  
</receiver> 

2.动态注册
Activity中,需在onStart()中调用registerReceiver()进行注册和在onStop中调用unregisterReceiver()释放服务
@Override  
protected void onStart(){  
    super.onStart();  
     
    receiver = new MusicReceiver();  
    IntentFilter filter = new IntentFilter();  
    filter.addAction("com.makk.receiver.musicReceiver");  
    this.registerReceiver(receiver, filter);  
}  
 
@Override  
protected void onStop(){  
    this.unregisterReceiver(receiver);  
     
    super.onStop();  
二种注册方式的比较:
静态注册方式,由系统来管理receiver,而且程序里的所有receiver,可以在xml里面一目了然
动态注册方式,隐藏在代码中,比较难发现;需要特别注意的是,在退出程序前要记得调用
Context.unregisterReceiver()方法。一般在activity的onStart()里面进行注册, onStop()里面进行注销。官方提醒,如果在Activity.onResume()里面注册了,就必须在Activity.onPause()注销。


五、BroadcastReceiver常用API
abortBroadcast()
这个方法可以截获由 sendOrderedBroadcast()发送来的 广播,让其它广播接收者无法收到这个广播。
clearAbortBroadcast()
这个方法是针对上面的 abortBroadcast() 方法的,用于取消截获广播。这样它的下一级广播接收者就能够收到该广播了。
getAbortBroadcast()
这个方法作用是:判断是否调用了 abortBroadcast(),如果先调用 abortBroadcast (),接着再调用 getAbortBroadcast (),将返回 true; 如果在调用 abortBroadcast ()、clearAbortBroadcast ()
getAbortBroadcast (),将返回 false;
getResultCode ()
如果用下面四个方法发送得广播,返回码为: -1 ;
// sendBroadcast(intent);
// sendBroadcast(intent, receiverPermission);
// sendOrderedBroadcast(intent, receiverPermission);
// sendStickyBroadcast(intent);
如果用下面两个方法发送得广播,返回码为:根据你设置 initialCode 的数字是多少就是多少;
// sendStickyOrderedBroadcast(intent, resultReceiver, scheduler,
// initialCode, initialData, initialExtras)
// sendOrderedBroadcast(intent, receiverPermission, resultReceiver,
// scheduler, initialCode, initialData, initialExtras)
getResultData ()
得到发送广播时设置的 initialData 的数据;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值