- 广播有两种注册方式:静态注册和动态注册。静态注册是在AndroidManifest中注册,在应用安装的时候会被系统解析,这种广播不需要应用启动就可以收到相应的广播。动态注册需要通过Context.registerReceiver()来注册,这种广播需要应用启动才能注册并接收广播。
- 实现不同组件之间的通信;一对多通信
- 与Activity组件不同的是,当系统通过Intent启动指定了Activity组件时,如果系统没有找到合适的Activity组件,会导致程序异常中止;但系统通过Intent激发BroadcastReceiver时,如果找不到合适的BroadcastReceiver组件,应用不会有任何问题
- BroadcastReceiver属于系统级的监听器,它拥有自己的进程,只要存在与之匹配的Broadcast被以Intent的形式发送出来,BroadcastReceiver就会被激活。当在系统注册一个BroadcastReceiver之后,每次系统以一个Intent的形式发布Broadcast的时候,系统都会创建与之对应的BroadcastReceiver广播接收者实例,并自动触发它的onReceive()方法,当onReceive()方法被执行完成之后,BroadcastReceiver的实例就会被销毁。
- 虽然它独自享用一个单独的进程,但也不是没有限制的,如果BroadcastReceiver.onReceive()方法不能在10秒内执行完成,Android系统就会认为该BroadcastReceiver对象无响应,然后弹出ANR(Application No Response)对话框,所以不要在BroadcastReceiver.onReceive()方法内执行一些耗时的操作。如果需要根据广播内容完成一些耗时的操作,一般考虑通过Intent启动一个Service(或其他组建)来完成该操作,而不应该在BroadcastReceiver中开启一个新线程完成耗时的操作,因为BroadcastReceiver本身的生命周期很短,可能出现的情况是子线程还没有结束,BroadcastReceiver就已经退出的情况,而如果BroadcastReceiver所在的进程结束了,该线程就会被标记为一个空线程,根据Android的内存管理策略,在系统内存紧张的时候,会按照优先级,结束优先级低的线程,而空线程无异是优先级最低的,这样就可能导致BroadcastReceiver启动的子线程不能执行完成。
由于涉及不同app,注意安全机制(查看官网(网)):
If you don't need to send broadcasts across applications, consider using this class with LocalBroadcastManager instead of the more general facilities described below. This will give you a much more efficient implementation (no cross-process communication needed) and allow you to avoid thinking about any security issues related to other applications being able to receive or send your broadcasts.
普通广播
Normal Broadcast
它是完全异步的,在逻辑上,当一个Broadcast被发出之后,所有的与之匹配的BroadcastReceiver都同时接收到Broadcast。
优点:传递效率比较高
缺点:一个BroadcastReceiver不能影响其他响应这条Broadcast的BroadcastReceiver
- BroadcastReceiver
发送方:
publicvoid broadcastIntent(View view)
{
Intent intent =newIntent();
intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
sendBroadcast(intent);
}
接收方:
//定义:
publicclassMyReceiver extends BroadcastReceiver{
@Override
publicvoid onReceive(Context context,Intent intent){
Toast.makeText(context,"Intent Detected.",Toast.LENGTH_LONG).show();
}
}
注册:
//静态注册:
<receiverandroid:name="MyReceiver">
<intent-filter>
<actionandroid:name="com.tutorialspoint.CUSTOM_INTENT">
</action>
</intent-filter>
</receiver>
//动态注册:
IntentFilter filter =newIntentFilter("com.tutorialspoint.CUSTOM_INTENT");
MyReceiver receiver =new MyReceiver();
Context.registerReceiver(receiver, filter );
Context.unregisterReceiver(receiver);
Note:
- If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead).
- Do not unregister in Activity.onSaveInstanceState(), because this won't be called if the user moves back in the history stack.
- LocalBroadcastManager
Helper to register for and send broadcasts of Intents to local objects within your process. This is has a number of advantages over sending global broadcasts with sendBroadcast(Intent)。
static LocalBroadcastManager | getInstance(Context context) |
void | registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
Register a receive for any local broadcasts that match the given IntentFilter.
|
boolean | sendBroadcast(Intent intent)
Broadcast the given intent to all interested BroadcastReceivers.
|
void | sendBroadcastSync(Intent intent)
Like
sendBroadcast(Intent) , but if there are any receivers for the Intent this function will block and immediately dispatch them before returning. |
void | unregisterReceiver(BroadcastReceiver receiver)
Unregister a previously registered BroadcastReceiver.
|
有序广播
Ordered Broadcast
它是同步执行的,有序广播的接收器将会按照预先声明的优先级依次接受Broadcast,是链式结构,优先级越高(-1000~1000),越先被执行。优先级高的接收器,可以把执行结果传入下一个接收器中,也可以终止Broadcast的传播(通过abortBroadcast()方法),一旦Broadcast的传播被终止,优先级低于它的接收器就不会再接收到这条Broadcast了。
Context.sendOrderedBroadcast():发送有序广播。(其他与上边同)
系统广播
拦截软件的原理
参考
Android--广播BroadcastReceiver (网)(BroadcastReceiver基础概念、种类、Demo)