Introduction of Broadcastreceiver
Broadcastreceiver, used for asynchronous reception of broadcast intent, broadcast intent by calling Context.sendbroadcast () send, Broadcastreceiver () receive.
The broadcast intent is implemented by calling Context.sendbroadcast (), Context.sendorderedbroadcast (), Context.sendstickybroadcast (). Usually a broadcast intent can be received by multiple broadcast receivers subscribing to this intent, and the broadcast receiver is similar to the topic message receiver in JMS.
Broadcast receivers can only receive broadcasts, respond to broadcast notifications, and many broadcasts are generated from system code, such as notification of time zone changes, low battery power, user-changed language preference, or power-on startup, etc.
Broadcast receivers do not have a user interface, but they can receive information to initiate an activity or use Notificationmanager to notify the user.
Broadcastreceiver Receive broadcast mode:
1. Normal broadcasts (regular broadcast), sent with Context.sendbroadcast () is completely asynchronous, and they all run in an undefined order, usually at the same time. This is more efficient, but it means that receiver cannot contain the results or aborted APIs that you want to use.
2. Ordered broadcasts (ordered broadcast), with Context.sendorderedbroadcast () sent each time sent to a receiver. The so-called order is that each receiver can be propagated to the next receiver after execution, or it can stop spreading completely--not to other receiver. The sequence of receiver runs can be controlled by the android:priority inside the matched Intent-filter, and priority in any order when the receiver priority is the same.
Second, Broadcastreceiver registration mode
1 Static Registration
Androidmanifest.xml, application inside, define the receiver and set the action to receive
<receiver android:name= ". Receiver. Musicreceiver "
>
<intent-filter>
<action android:name=" Com.homer.receiver.musicReceiver "/>
</intent-filter>
</receiver>
2 Dynamic Registration
In an activity, you need to call Registerreceiver () in OnStart () to register and invoke the Unregisterreceiver () release service in OnStop
Private Musicreceiver receiver;
@Override
protected void OnStart () {
super.onstart ();
Receiver = new Musicreceiver ();
Intentfilter filter = new Intentfilter ();
Filter.addaction ("Com.homer.receiver.musicReceiver");
This.registerreceiver (receiver, filter);
@Override
protected void OnStop () {
this.unregisterreceiver (receiver);
Super.onstop ();
}
Comparison of 32 kinds of registration methods
Static registration mode, by the system to manage receiver, and the program in all receiver, can be in the XML inside a glance
Dynamic registration, hidden in the code, more difficult to find; Special attention is required to call the Context.unregisterreceiver () method before exiting the program. Generally in the activity of the onstart () inside to register, OnStop () inside for cancellation. The official reminder that if you register in Activity.onresume (), you must log off at Activity.onpause ().
Three, Broadcastreceiver life cycle
A Broadcastreceiver object is valid only if it is invoked onreceive (context, Intent), and when returned from the function, the object is invalid, ending the lifecycle.
Therefore, it can be seen from this feature that in the invoked OnReceive (context, Intent) function, there can be no too time-consuming operation, can not use the thread to execute. For time-consuming operations, it should be done in StartService. Because the broadcastreceiver may not be valid when the result returned by another asynchronous operation is obtained.