Android Broadcast Receiver (广播接收者)

一、简介

  Broadcast Receiver是广播接收器,接收自定义和系统的主播。也可以称为监听器。

  Broadcast Intent与Intent一样是通信的媒介,与Intent不同的是Broadcast Intent同时被多个组件接收。

  Broadcast Intent广播机制,广播源发出消息,通过AMS(Activity manager service),可以使多个组件接收同一个消息。与Broadcast Intent成对配合应用的是Broadcast Receiver广播接收器,用于,接收Broadcast Intent广播出的消息。

  Intent与Broadcast Intent对比:

                (示图来源于Android权威指南)

二、Broadcast Receiver注册方式

  1. 静态注册

    静态注册是在AndroidManifest.xml文件中,通过标签<receiver>进行注册,使用标签<intentfilter>设置需要过滤的消息的intent。此种注册方式的生命周期是依随应用程序,在程序结束后接收到消息,程序会自动运行并被接收器事件执行。

 1 <!-- 广播接收类名 -->
 2 <!-- exported设置此broadcast receiver 能否接收其它应用的广播消息 -->
 3 <!-- permission是此广播的权限,只能接收符合权限应用的广播 -->
 4 <receiver
 5     android:name=".TimeBroadCastReceiver"
 6     android:exported="false"
 7     android:permission="com.example.RECEIVER_DEMO">
 8     <!-- priority 有序广播的优先级别 -->
 9     <intent-filter android:priority="100">
10         <!-- 广播消息名称 -->
11         <action android:name="android.intent.action.ACTION_TIME_TICK" />
12     </intent-filter>
13 </receiver>

 

  (1). exported是设置是否接收其它应用发出的广播,默认值是通过是否有intent-filter为设置,有intent-filter为true,反之为false。当为true时,接收其它应用广播消息。

  (2).android:permission是设置广播权限,只有接收申请了此权限的应用的广播。

 

  2. 动态注册

    动态注册是使用registerReceiver(...)方法进行注册,在此种注册方式中,Broadcast的生命周期与应用程序一样,在程序结束时,即结束。也可通过unRegisterReceiver(...)方法注销Broadcast。

 

 1 // 接收者收到消息后,处理函数回调handler
 2 mTimeReceiver = new TimeBroadCastReceiver(mHandler);
 3 IntentFilter intentFilter = new IntentFilter();
 4 // 广播消息名称
 5 intentFilter.addAction(Intent.ACTION_TIME_TICK);
 6 intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
 7 // 有序广播优先级别
 8 intentFilter.setPriority(100);
 9 // 注册广播类
10 registerReceiver(mTimeReceiver, intentFilter);

   3. 自定义权限

1 <!-- 自定义权限名称,即注册一个新的权限 -->
2 <permission android:name="com.example.RECEIVER_DEMO"/>
3 <!-- 申请权限 -->
4 <uses-permission android:name="com.example.RECEIVER_DEMO"/>

 

  PS:这两种注册方式的区别在于生命周期的不同,在系统中,一些广播消息只能使用动态注册方式,比如:监听时间。

三、Broadcast Receiver 发送方式

  1. 普通(异步)广播方式

    普通广播方式,也可以称作异步广播,普通广播的消息可以被所有接收者收到,消息传递效率比较高,但缺点是不能将接收消息后的结果传递给下一个接收者,并且不能中止广播Intent的广播。

 

1 Intent intent = new Intent();
2 intent.setAction(Intent.ACTION_TIME_TICK);
3 intent.setAction(Intent.ACTION_TIME_CHANGED);
4 sendBroadcast(intent);

 

  2. 有序广播方式

    有序广播通过接收者设置的优先级别(优先级别取值范围-1000~1000之间,1000最高),依次接收广播消息。在接收到广播消息后,可以在Broadcast Intent添加此次接收者的一些数据,传递给下一个广播接收者。但是,此种方式的消息传递效率比较低。

 

1 Intent intent = new Intent();
2 // 消息名称
3 intent.setAction(Intent.ACTION_TIME_TICK);
4 intent.setAction(Intent.ACTION_TIME_CHANGED);
5 // receiverPermission 消息权限
6 sendOrderedBroadcast(intent, receiverPermission);

 

  总结:

    1. 普通广播:异步,消息传递效率高,数据不能共享。

    2. 有序广播:同步,消息传递效率低,数据能共享。

 

四、接收者实现

 1 public class TimeBroadCastReceiver extends BroadcastReceiver
 2 {
 3     protected static final String TAG = "TimeBroadCastReceiver";
 4 
 5     @Override
 6     public void onReceive(Context context, Intent intent)
 7     {
 8         switch (intent.getAction())
 9         {
10             case Intent.ACTION_TIME_TICK:
11                 SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss     ");
12                 Date curDate = new Date(System.currentTimeMillis());
13                 String str = formatter.format(curDate);
14                 Log.d(TAG, str);
15                 // 接收到广播后,中止广播
16                 abortBroadcast();
17                 break;
18         }
19     }
20 }

 

注意:onReceive()方法必须在10秒内完成,如果超出时间,则会抛出“Application No Response”。当onReceive()需要长时间执行时,则需要使用Service来实现(context.startService())或者创建子线程实现。

PS:在多个应用中注册广播,并添加广播接收事件,广播类型和Action相同,即使添加相同权限,多个应用都可以接收此消息。实现仅本应用接收的广播,可以使用LocalBroadcastManager注册广播为本地(限本应用)广播;或者设置exported属性。

 

posted @ 2017-05-07 14:55  naray  阅读(600)  评论(0编辑  收藏  举报