美文网首页
四大组件---BroadcastReceiver

四大组件---BroadcastReceiver

作者: 瀚海来客 | 来源:发表于2019-10-16 11:35 被阅读0次

BroadcastReceiver的发送方式和区别

https://www.jianshu.com/p/ea5e233d9f43

  静态注册:是常驻型,也就是说当应用程序关闭后,如果有信息广播来,程序也会被系统调用自动运行。
  动态注册:不是常驻型广播,也就是说广播跟随程序的生命周期。

    有序广播:
        通过mContext.sendOrderedBroadcast(Intent, String, BroadCastReceiver, Handler, int, String, Bundle)发送的是有序广播。
        Intent intent = new  Intent();
        //设置intent的动作为com.example.broadcast,可以任意定义
        intent.setAction("com.example.broadcast");
        //发送无序广播
        //第一个参数:intent
        //第二个参数:String类型的接收者权限
        //第三个参数:BroadcastReceiver 指定的接收者
        //第四个参数:Handler scheduler
        //第五个参数:int 此次广播的标记 
        //第六个参数:String 初始数据
        //第七个参数:Bundle 往Intent中添加的额外数据
        sendOrderedBroadcast(intent, null, null, null, "这是初始数据", );
    无序广播:
          通过mContext.sendBroadcast(Intent)或mContext.sendBroadcast(Intent, String)发送的是无序广播(后者加了权限);
    区别:
        无序广播:所有的接收者都会接收事件,不可以被拦截,不可以被修改。
        有序广播:按照优先级,一级一级的向下传递,接收者可以修改广播数据,也可以终止广播事件。

相关文章

网友评论

      本文标题:四大组件---BroadcastReceiver

      本文链接:https://www.haomeiwen.com/subject/dbvymctx.html