赞
踩
是典型观察者模式的应用,基于消息发布者/消息订阅者模型实现的。
原理描述
1、广播发布者:sendBroadcast、sendOrderedBroadcast
2、广播接受者:BroadcastReceiver
注册广播接收器有两种方式:静态注册和动态注册。
在AndroidMainfest文件中声明即可,属性如下:
<receiver
android:enabled=["true" | "false"]
//此broadcastReceiver能否接收其他App的发出的广播
//默认值是由receiver中有无intent-filter决定的:如果有intent-filter,默认值为true,否则为false
android:exported=["true" | "false"]
android:icon="drawable resource"
android:label="string resource"
//继承BroadcastReceiver子类的类名
android:name=".mBroadcastReceiver"
//具有相应权限的广播发送者发送的广播才能被此BroadcastReceiver所接收;
android:permission="string"
//BroadcastReceiver运行所处的进程
//默认为app的进程,可以指定独立的进程
//注:Android四大基本组件都可以通过此属性指定自己的独立进程
android:process="string" >
//用于指定此广播接收器将接收的广播类型
//本示例中给出的是用于接收网络状态改变时发出的广播
<intent-filter>
<action android:name="android.net.conn.CONNETIVITY_CHANGE"/>
</intent-filter >
</receiver>
静态注册,在App首次启动并会实例化BroadcastReceiver类,同时广播接受器也会注册到AMS中。也就是说App一启动会回调onReceiver方法。
提升广播的优先级可在intent-filter中设置属性priority,值越大优先级就越高。
<receiver android:name=".MyBroadcastReceiver">
<intent-filter android:priority="100">
</intent-filter>
</receiver>
使用Content的registerReceiver方法注册
public abstract Intent registerReceiver(BroadcastReceiver receiver,
IntentFilter filter, @Nullable String broadcastPermission,
@Nullable Handler scheduler, @RegisterReceiverFlags int flags);
上面是registerReceiver方法中最多参数的一个,其中receiver和filter必传的,其他是可选
//注册广播
mReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter();
//必须action的匹配规则,否则无法接收到消息,在发送广播的意图中必须匹配action的字符串
filter.addAction("test");
registerReceiver(mReceiver,filter);
//发送广播
Intent intent = new Intent();
intent.setAction("test");
sendBroadcast(intent);
有注册,必然就会注销,一般onDestroy或者onPause中注销,如果不注销会引起内存泄漏
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
在动态注册时,需注意,不能重复new MyBroadcastReceiver()对象进行注册,否则会多次回调onReceiver方法,带来一些额外的问题。
1、首页注册广播接收器(BroadcastReceiver),针对动态注册的方式,需设置匹配字段action,否则无法收到广播
mReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("test");
registerReceiver(mReceiver,filter);
//或者
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="test"/>
</intent-filter>
</receiver>
2、通过Intent意图发送广播,意图必须添加action相同的字符串(与注册的一样),否则无法收到广播
Intent intent = new Intent();
intent.setAction("test");
sendBroadcast(intent);
3、在接受到了广播,会主动回调BroadcastReceiver中的onReceive方法
public class MyBroadcastReceiver extends BroadcastReceiver{
private static final String TAG = "BroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: "+intent.getAction());
}
}
在Android系统内置很多系统广播,当系统状态信息发生了改变,便会发出相应的广播,比如开机、电量变化、网络变化……前面我们知道了在注册和发送广播需要相同的action,而系统广播同样有不同的action,Android系统广播action如下:
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<!-- 应用添加 -->
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<!-- 应用删除 -->
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<!-- 应用替换 -->
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<!-- 应用重新启动 -->
<action android:name="android.intent.action.PACKAGE_RESTARTED"/>
<!--必须设置scheme,相当于URI,不然收不到系统广播-->
<data android:scheme="package"/>
</intent-filter>
</receiver>
当使用系统广播时,只需要在注册广播接收者时定义相关的action即可,并不需要手动发送广播,当系统有相关操作时会自动进行系统广播
也可以以动态注册方式:
mReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.PACKAGE_REMOVED");
filter.addAction("android.intent.action.PACKAGE_ADDED");
filter.addDataScheme("package");
registerReceiver(mReceiver,filter);
同一时刻同一条广播被一个广播接收器接收到这条消息后,必须先执行完当前广播接收器的业务逻辑后,才会继续传递广播。是有先后顺序接收的,接收规则如下:
如果某条广播被中断了,后面的广播接收器将无法收到广播
例子:
1、广播接收器
public class MyBroadcastReceiver extends BroadcastReceiver{
private static final String TAG = "BroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: "+intent.getAction());
for (int i = 0; i < 5; i++) {
Log.e(TAG, "onReceive: "+i);
}
}
}
public class MyBroadcastReceiver2 extends BroadcastReceiver{
private static final String TAG = "BroadcastReceiver2";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive2: "+intent.getAction());
}
}
注册广播
//注册广播
<receiver android:name=".MyBroadcastReceiver">
<intent-filter android:priority="100">
<action android:name="test"/>
</intent-filter>
</receiver>
//注册广播2
<receiver
android:name=".MyBroadcastReceiver2">
<intent-filter android:priority="80">
<action android:name="test"/>
</intent-filter>
</receiver>
//或者
mReceiver = new MyBroadcastReceiver();
mReceiver2 = new MyBroadcastReceiver2();
IntentFilter filter = new IntentFilter();
filter.addAction("test");
filter.setPriority(100);
registerReceiver(mReceiver,filter);
filter.setPriority(80);
registerReceiver(mReceiver2,filter);
发送广播
Intent intent = new Intent();
intent.setAction("test");
sendOrderedBroadcast(intent,null);
输出结果:
有序广播的使用过程与普通广播非常类似,差异仅在于广播的发送方式不同:sendOrderedBroadcast,判断是否是有序广播,可以在onReceive中调用isOrderedBroadcast方法即可,true则是;如果需要中断广播传递,可在onReceive中调用abortBroadcast方法。
在Android中,广播默认是可以跨App(跨进程)直接通信。因为exported属性默认是true
由于广播默认是跨进程,同时广播是根据发送意图Intent和意图过滤器Intent-filter进行匹配判断,从而决定BroadcastReceiver的onReceiver方法是否回调,这可能会出现以下问题:
针对上面的问题,有以下解决方案:
<receiver android:name=".MyBroadcastReceiver"
android:exported="false">
<intent-filter android:priority="100">
<action android:name="test"/>
</intent-filter>
</receiver>
<!--1、自定义新权限-->
<!--自定义新权限-->
<permission android:name="com.hzw.MY_RECEIVER"/>
<application
……
<!-- 2、发送方必须要拥有com.hzw.permission.MY_RECEIVER权限,接受器才会接受-->
<receiver android:name=".MyBroadcastReceiver"
android:permission="com.hzw.permission.MY_RECEIVER">
<intent-filter android:priority="100">
<action android:name="test"/>
</intent-filter>
</receiver>
……
</application>
2、使用本地广播,系统封装好的LocalBroadcastManager类
mReceiver2 = new MyBroadcastReceiver2();
IntentFilter filter = new IntentFilter();
filter.addAction("test");
//获取LocalBroadcastManager类的实例,通过该实例注册广播
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
mLocalBroadcastManager.registerReceiver(mReceiver2,filter);
//注销也需通过LocalBroadcastManager进行注销
mLocalBroadcastManager.unregisterReceiver(mReceiver2);
对于LocalBroadcastManager方式发送的应用内广播,只能通过LocalBroadcastManager动态注册,不能静态注册。
https://blog.csdn.net/carson_ho/article/details/52973504
https://blog.csdn.net/javensun/article/details/7334230
http://h529820165.iteye.com/blog/1656778
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。