Broadcast Receiver
The Android broadcast mechanism consists of three basic elements: Broadcast (broadcast)-used to send broadcasts, broadcast receivers (Broadcastreceiver)-for receiving broadcasts, intent content (Intent)-the medium used to hold information about the broadcast. Broadcast is a widely used mechanism for transmitting information between applications. Broadcastreceiver is a class of components that are filtered to receive and respond to the sent broadcast.
<p>
1. Introduction of Android broadcasting mechanismThere are all kinds of broadcasts in Android, such as the use of batteries, the reception of telephones and the reception of text messages, which generate a broadcast, and application developers can listen to these broadcasts and make procedural logic.
2, the role of Broadcastreceiver
The Broadcastreceiver (broadcast receiver) is a component that is provided for system broadcast, and the broadcast event handling mechanism is system-level.
3. How to write Broadcastreceiver
Inherits the Broadcastreceiver class, overrides the Onreceiver method, and processes the broadcast in the Onreceiver method.
4. Broadcastreveiver life Cycle A Broadcastreceiver object is only valid when called OnReceive (Context, Intent), and when returned from the function, the object is invalid and ends the life cycle.
The method of registering Broadcastreceiver Broadcastreceiver is used to monitor the broadcast event (Intent) in order to achieve this, broadcastreceiver must be registered, the method of registration has The following two types:
1. Register Broadcastreceiver:registerreceiver (Receiver,filter) in the code of the application; Unregister broadcastreceiver:unregisterreceiver (receiver); If a broadcastreceiver is used to update the UI, it is usually registered using this method, registering the Broadcastreceiver when Activty is started, and then canceling the registration after the activity is not visible (OnStop ()). 2, in the Androidmainfest.xml register in such a way registered broadcastreceiver, even if the application does not start, or be killed, will always remain active;
Third, Android built-in broadcast Actions in the Android platform, built up a lot of action to help developers to listen to the various events that occur on the phone, the following is a more common action. You can find its constants in the Help document, intent class, with acition (the key is broadcast Action)
try not to handle too many logical problems in broadcast, and suggest complex logic to the activity or Service to handle
"Android Beginner" 13, broadcast Receiver