Android查缺补漏--BroadcastReceiver的类型与使用

Broadcast 是一种被用于应用内和应用之间传递信息的机制。一个广播可以对应多个接受者。一个完整的广播机制,需要具有以下三个要素:

  • 发送广播的Broadcast
  • 接受广播的BroadcastReceiver
  • 传递信息的Intent

广播的注册分为静态注册和动态注册:

  • 静态注册:静态注册的广播是指在AndroidManifest中注册的广播,此种广播在应用安装时就被系统解析,不需要启动应用就可以收到相应的广播。
<receiver android:name=".broadcast.MyBroadcastReceiver">
    <intent-filter>
        <action android:name="MY_BROADCAST_RECEIVER" />
    </intent-filter>
</receiver>
  • 动态注册:通过Context.registerReceiver()来实现,不需要时要通过Context.unRegisterReceiver()解除广播,此种广播必须应用启动后才能注册并接收广播。 // 动态注册广播接收器 registerReceiver(new DynamicBroadcastReceiver(), new IntentFilter(MyBroadcastReceiver.ACTION));

广播又分为普通广播、有序广播、本地广播和sticky广播。

一、普通广播

普通广播通过Context.sendBroadcast()发送,我们没有办法制定Receiver们对于普通广播的接收顺序。理论上所有的接收器(Receiver)接收到广播的顺序不确定,但一般是按照其在AndroidMainfest.xml文件中注册的顺序(不绝对)。 普通广播中,接受者不能将处理结果传递给下一个接收器,也无法终止广播的传播。

如下代码是一个静态注册的广播示例:

public class MyBroadcastReceiver extends BroadcastReceiver {

    String TAG = MyBroadcastReceiver.class.getSimpleName();

    public static final String ACTION = "MY_BROADCAST_RECEIVER";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "接收到广播消息:" + intent.getStringExtra(BroadcastTestActivity.INTENT_INFO));
    }
}

然后再AndroidMainfest.xml中注册这个广播:

<receiver android:name=".broadcast.MyBroadcastReceiver">
    <intent-filter>
        <action android:name="MY_BROADCAST_RECEIVER" />
    </intent-filter>
</receiver>

接下来在Activity中调用Context.sendBroadcast()发送广播就可以了:

Intent intent = new Intent(MyBroadcastReceiver.ACTION);
intent.putExtra(INTENT_INFO, "我是一个普通广播");
sendBroadcast(intent);

log如下:

12-08 17:29:44.259 6644-6644/cn.codingblock.androidadvancestudy I/MyBroadcastReceiver: 接收到广播消息:我是一个普通广播
  • 普通广播的接收顺序测试 我们仿照MyBroadcastReciver创建多个接收器,代码一样:
/**
 * 静态注册的广播接收器2
 * Created by liuwei on 17/12/7.
 */
public class MyBroadcast2Receiver extends BroadcastReceiver {

    String TAG = MyBroadcast2Receiver.class.getSimpleName();

    public static final String ACTION = "MY_BROADCAST_RECEIVER";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "接收到广播消息:" + intent.getStringExtra(BroadcastTestActivity.INTENT_INFO));
    }
}
/**
 * 静态注册的广播接收器3
 * Created by liuwei on 17/12/7.
 */
public class MyBroadcast3Receiver extends BroadcastReceiver {...}
/**
 * 静态注册的广播接收器4
 * Created by liuwei on 17/12/7.
 */
public class MyBroadcast4Receiver extends BroadcastReceiver {...}
/**
 * 静态注册的广播接收器5
 * Created by liuwei on 17/12/7.
 */
public class MyBroadcast5Receiver extends BroadcastReceiver {...}
/**
 * 静态注册的广播接收器6
 * Created by liuwei on 17/12/7.
 */
public class MyBroadcast6Receiver extends BroadcastReceiver {...}

然后在AndroidMainfest.xml中为以上广播都注册同一个action

<receiver android:name=".broadcast.MyBroadcastReceiver">
    <intent-filter>
        <action android:name="MY_BROADCAST_RECEIVER" />
    </intent-filter>
</receiver>

<receiver android:name=".broadcast.MyBroadcast6Receiver">
    <intent-filter>
        <action android:name="MY_BROADCAST_RECEIVER" />
    </intent-filter>
</receiver>

<receiver android:name=".broadcast.MyBroadcast2Receiver">
    <intent-filter>
        <action android:name="MY_BROADCAST_RECEIVER" />
    </intent-filter>
</receiver>

<receiver android:name=".broadcast.MyBroadcast3Receiver">
    <intent-filter>
        <action android:name="MY_BROADCAST_RECEIVER" />
    </intent-filter>
</receiver>

<receiver android:name=".broadcast.MyBroadcast4Receiver">
    <intent-filter>
        <action android:name="MY_BROADCAST_RECEIVER" />
    </intent-filter>
</receiver>

<receiver android:name=".broadcast.MyBroadcast5Receiver">
    <intent-filter>
        <action android:name="MY_BROADCAST_RECEIVER" />
    </intent-filter>
</receiver>

点击发送广播,查看log:

12-08 17:29:44.259 6644-6644/cn.codingblock.androidadvancestudy I/MyBroadcastReceiver: 接收到广播消息:我是一个普通广播
12-08 17:29:44.268 6644-6644/cn.codingblock.androidadvancestudy I/MyBroadcast6Receiver: 接收到广播消息:我是一个普通广播
12-08 17:29:44.271 6644-6644/cn.codingblock.androidadvancestudy I/MyBroadcast2Receiver: 接收到广播消息:我是一个普通广播
12-08 17:29:44.273 6644-6644/cn.codingblock.androidadvancestudy I/MyBroadcast3Receiver: 接收到广播消息:我是一个普通广播
12-08 17:29:44.277 6644-6644/cn.codingblock.androidadvancestudy I/MyBroadcast4Receiver: 接收到广播消息:我是一个普通广播
12-08 17:29:44.280 6644-6644/cn.codingblock.androidadvancestudy I/MyBroadcast5Receiver: 接收到广播消息:我是一个普通广播

二、有序广播

在AndroidMainfest.xml中注册广播时通过priority(值越优先级越高)节点为广播添加优先级,然后再用Context.sendOrderedBroadcast()发送,接收者们就会按照优先级顺序依次执行。

有序广播的接收者和给下一个接收者传递数据,并且接收者在收到广播之后可以抛弃广播,使广播不再向后传递。

为上面6个接收器添加优先级:

<receiver android:name=".broadcast.MyBroadcastReceiver">
    <intent-filter android:priority="1">
        <action android:name="MY_BROADCAST_RECEIVER" />
    </intent-filter>
</receiver>

<receiver android:name=".broadcast.MyBroadcast6Receiver">
    <intent-filter android:priority="6">
        <action android:name="MY_BROADCAST_RECEIVER" />
    </intent-filter>
</receiver>

<receiver android:name=".broadcast.MyBroadcast2Receiver">
    <intent-filter android:priority="2">
        <action android:name="MY_BROADCAST_RECEIVER" />
    </intent-filter>
</receiver>

<receiver android:name=".broadcast.MyBroadcast3Receiver">
    <intent-filter android:priority="3">
        <action android:name="MY_BROADCAST_RECEIVER" />
    </intent-filter>
</receiver>

<receiver android:name=".broadcast.MyBroadcast4Receiver">
    <intent-filter android:priority="4">
        <action android:name="MY_BROADCAST_RECEIVER" />
    </intent-filter>
</receiver>

<receiver android:name=".broadcast.MyBroadcast5Receiver">
    <intent-filter android:priority="5">
        <action android:name="MY_BROADCAST_RECEIVER" />
    </intent-filter>
</receiver>

然后通过sendOrderedBroadcast发送广播观察log:

intent = new Intent(MyBroadcastReceiver.ACTION);
intent.putExtra(INTENT_INFO, "我是一个有序广播");
sendOrderedBroadcast(intent, null);

log如下:

12-08 18:17:26.455 25919-25919/cn.codingblock.androidadvancestudy I/MyBroadcast6Receiver: 接收到广播消息:我是一个有序广播
12-08 18:17:26.462 25919-25919/cn.codingblock.androidadvancestudy I/MyBroadcast5Receiver: 接收到广播消息:我是一个有序广播
12-08 18:17:26.464 25919-25919/cn.codingblock.androidadvancestudy I/MyBroadcast4Receiver: 接收到广播消息:我是一个有序广播
12-08 18:17:26.465 25919-25919/cn.codingblock.androidadvancestudy I/MyBroadcast3Receiver: 接收到广播消息:我是一个有序广播
12-08 18:17:26.466 25919-25919/cn.codingblock.androidadvancestudy I/MyBroadcast2Receiver: 接收到广播消息:我是一个有序广播
12-08 18:17:26.467 25919-25919/cn.codingblock.androidadvancestudy I/MyBroadcastReceiver: 接收到广播消息:我是一个有序广播
  • abortBroadcast()抛弃广播: 普通的广播是没有办法抛弃的,否则会抛出RuntimeException的异常。

只有有序广播才可以通过此方法抛弃。我们在MyBroadcast6Receiver中添加abortBroadcast()方法:

public class MyBroadcast6Receiver extends BroadcastReceiver {

    String TAG = MyBroadcast6Receiver.class.getSimpleName();

    public static final String ACTION = "MY_BROADCAST_RECEIVER";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "接收到广播消息:" + intent.getStringExtra(BroadcastTestActivity.INTENT_INFO));
        abortBroadcast();
        Log.i(TAG, "丢弃广播");
    }
}

然后点击发送有序广播,log如下:

12-08 18:34:27.989 1329-1329/cn.codingblock.androidadvancestudy I/MyBroadcast6Receiver: 接收到广播消息:我是一个有序广播
12-08 18:34:27.989 1329-1329/cn.codingblock.androidadvancestudy I/MyBroadcast6Receiver: 丢弃广播

可以看到广播已经被丢弃了。

  • setResult()传递给下一个接收者结果。
  • getResult()接收上一个接收者的结果。

在MyBroadcast6Receiver中添加setResult方法,在MyBroadcast5Receiver添加getResult方法:

public class MyBroadcast6Receiver extends BroadcastReceiver {

    String TAG = MyBroadcast6Receiver.class.getSimpleName();

    public static final String ACTION = "MY_BROADCAST_RECEIVER";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "接收到广播消息:" + intent.getStringExtra(BroadcastTestActivity.INTENT_INFO));
//        abortBroadcast();
//        Log.i(TAG, "丢弃广播");
        setResult(006, "我是老6传来的消息", null);
    }
}
public class MyBroadcast5Receiver extends BroadcastReceiver {

    String TAG = MyBroadcast5Receiver.class.getSimpleName();

    public static final String ACTION = "MY_BROADCAST_RECEIVER";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "接收到广播消息:" + intent.getStringExtra(BroadcastTestActivity.INTENT_INFO));
        String data = getResultData();
        Log.i(TAG, "data=" + data);
    }
}

log如下:

12-08 18:40:01.415 10372-10372/cn.codingblock.androidadvancestudy I/MyBroadcast6Receiver: 接收到广播消息:我是一个有序广播
12-08 18:40:01.434 10372-10372/cn.codingblock.androidadvancestudy I/MyBroadcast5Receiver: 接收到广播消息:我是一个有序广播
12-08 18:40:01.434 10372-10372/cn.codingblock.androidadvancestudy I/MyBroadcast5Receiver: data=我是老6传来的消息
12-08 18:40:01.440 10372-10372/cn.codingblock.androidadvancestudy I/MyBroadcast4Receiver: 接收到广播消息:我是一个有序广播
12-08 18:40:01.442 10372-10372/cn.codingblock.androidadvancestudy I/MyBroadcast3Receiver: 接收到广播消息:我是一个有序广播
12-08 18:40:01.445 10372-10372/cn.codingblock.androidadvancestudy I/MyBroadcast2Receiver: 接收到广播消息:我是一个有序广播
12-08 18:40:01.447 10372-10372/cn.codingblock.androidadvancestudy I/MyBroadcastReceiver: 接收到广播消息:我是一个有序广播

三、本地广播

以上的广播对于系统来说是全局的,发出广播后,系统内的应用只要注册了相应的接收器就都可以收到广播。如果我们想在本应用发出的广播只在此应用内可以收到,那么可以使用本地广播了。

本地广播由 LocalBroadcastManager 管理,是在 API 21 以后添加的,使用起来也很方便,需要先通过 LocalBroadcastManager.getInstance() 方法获取其单例,剩下的用法与其他广播类似,其主要方法有以下几个:

  • registerReceiver():注册广播接收器。
  • unregisterReceiver():解除广播接收器。
  • sendBroadcast():发送异步广播。
  • sendBroadcastSync():发送同步广播。

使用本地广播时,无需在AndroidMainfest.xml中注册,必须使用 LocalBroadcastManager.getInstance(...).registerReceiver(..)来注册接收器。

我们来写个本地广播的小栗子,首先注册两个本地广播:

LocalBroadcastManager.getInstance(context).registerReceiver(new MyBroadcastReceiver(), new IntentFilter(MyBroadcastReceiver.ACTION));
LocalBroadcastManager.getInstance(context).registerReceiver(new MyBroadcast2Receiver(), new IntentFilter(MyBroadcastReceiver.ACTION));

然后发送本地广播:

intent.putExtra(INTENT_INFO, "我是一个本地广播");
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

log如下:

12-09 17:20:47.799 15019-15019/cn.codingblock.androidadvancestudy I/MyBroadcastReceiver: 接收到广播消息:我是一个本地广播
12-09 17:20:47.799 15019-15019/cn.codingblock.androidadvancestudy I/MyBroadcast2Receiver: 接收到广播消息:我是一个本地广播

四、sticky广播(不建议使用)

sticky广播会一直处于滞留状态,sticky广播被发出后,只要有能够匹配其的新接收器被注册了就可以收到广播,sticky广播通过Context.sendStickyBroadcast()发送。

最后想说的是,本系列文章为博主对Android知识进行再次梳理,查缺补漏的学习过程,一方面是对自己遗忘的东西加以复习重新掌握,另一方面相信在重新学习的过程中定会有巨大的新收获,如果你也有跟我同样的想法,不妨关注我一起学习,互相探讨,共同进步!

参考文献:

  • 《Android开发艺术探索》
  • 《Android开发进阶从小工到专家》

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏编程札记

tornado配合celery及rabbitmq实现web request异步非阻塞

2795
来自专栏开发 & 算法杂谈

也谈应用程序级的同步、异步、阻塞、非阻塞

这几个经常出现的词汇很容易会引起大家的误解,第一次接触相关词汇是在课上,当时上操作系统的老师说得比较模糊(阻塞==同步,非阻塞==异步),当时觉得挺对 的,

682
来自专栏.NET技术

.net平台的rabbitmq使用封装

  RabbitMq大家再熟悉不过,这篇文章主要整对rabbitmq学习后封装RabbitMQ.Client的一个分享。文章最后,我会把封装组件和demo奉上。

1047
来自专栏Java3y

数据链路层

数据链路 (data link) 除了物理线路外,还必须有通信协议来控制这些数据的传输。若把实现这些协议的硬件和软件加到链路上,就构成了数据链路。

630
来自专栏大内老A

WCF技术剖析之三十一:WCF事务编程[上篇]

WCF事务编程其实很简单,可以用三句话进行概括:通过服务契约决定事物流转(Transaction Flow)的策略;通过绑定实施事务的流转;通过服务行为控制事务...

1735
来自专栏向治洪

xmpp即时通讯详解

摘要:         此文档定义了可扩展消息出席协议(XMPP)的核心特性:协议使用XML元素在任意两个网络端点间近实时的交换结构化信息。当XMPP为交换X...

2445
来自专栏程序你好

服务定位器模式(Service Locator Pattern)介绍

892
来自专栏Java技术

为什么分布式一定要有Redis?

考虑到绝大部分写业务的程序员,在实际开发中使用 Redis 的时候,只会 Set Value 和 Get Value 两个操作,对 Redis 整体缺乏一个认知...

942
来自专栏Spark生态圈

[spark] 从spark-submit开始解析整个任务调度流程

spark应用程序可以以Client模式和Cluster启动,区别在于Client模式下的Driver是在执行spark-submit命令节点上启动的,而Clu...

842
来自专栏扎心了老铁

zookeeper curator使用caches实现各种监听

1、篇首语 curator是zookeeper的一个高级api开发包。封装了zookeeper众多的recipes,并且实现了一些新的recipes原语,最重要...

3965

扫码关注云+社区