1.BroadcastReceiver Introduction:
The Broadcastreceiver broadcast receiver, which is essentially a global listener for listening to global broadcast (broadcast) messages, makes it very easy to communicate between different components of the system.
Broadcastreceiver this listener is different from the normal Onxxxlistener listener, Onxxxlistener is an application-level listener, and the listener ends when the program exits. The Broadcastreceiver (registered in the config file) is a system-level listener that has its own process, and broadcastreceiver is activated as long as there is a matching broadcast that is passed in intent form.
Unlike the activity, when the system starts the specified activity component through intent, if the system does not find the target activity component, a program exception interrupt occurs, but when the system activates broadcastreceiver through intent, The system will not have any problems even when the target broadcastreceiver is not found
2. Create Broadcastreceiver
2.1 First need to inherit the Broadcastreceiver class, overriding the OnReceive () method inside this class, the code is as follows:
1 Packagecom.example.administrator.broadcastreceivertest;2 3 ImportAndroid.content.BroadcastReceiver;4 ImportAndroid.content.Context;5 Importandroid.content.Intent;6 ImportAndroid.util.Log;7 ImportAndroid.widget.Toast;8 9 Ten Public classMybroadcastreceiverextendsbroadcastreceiver{ One PublicMybroadcastreceiver () { ALOG.I ("tag", "Create constructor ..."); - } - the @Override - Public voidOnReceive (Context context, Intent Intent) { -String msg = Intent.getstringextra ("msg")); - Toast.maketext (Context,msg,toast.length_short). Show (); + } -}
When a broadcastreceiver is activated, a Broadcastreceiver instance is created for it, and the OnReceive () method is automatically triggered, and after the OnReceive () method is executed, Broadcastreceiver instances will be destroyed. If the Broadcastreceiver onreceive () method cannot be completed within 10s, Android will consider the program unresponsive, so do not take a time-consuming action in the OnReceive () method, or the ANR (application No Response) dialog box.
If you need to take a time-consuming action in the OnReceive () method, consider starting a service with intent to complete the operation, and you should not consider starting a new thread to handle the time-consuming operation because the Broadcastreceiver has a short life cycle, Perhaps the child thread is not over yet, Broadcastreceiver has exited. If the process where the Broadcastreceiver is located ends, the thread is marked as a null thread. According to the Android memory management policy, when the system memory is tight, priority is first terminated according to priority, while the empty thread priority is undoubtedly the lowest priority, after the system is recycled, the thread will not be able to complete the related operation
2.2 Register Broadcastreceiver
There are two ways to register Broadcastreceiver:
1> is specified using code (dynamic registration), invoking the Registerreceiver (Broadcastreceiver receiver,intentfilter Filter) provided by the context class;
New mybroadcastreceiver (); 2 New Intentfilter (show_message); 3 Registerreceiver (Mybroadcastreceiver,filter);
Intentfilter is based on Show_message used to match broadcast, Show_message is a custom static string constant.
Note that the place where Broadcastreceiver is registered, Google officials said:
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 would cut down on unnecessary system overhead). Do Activity.onSaveInstanceState() is unregister in, because this won ' is called if the user moves back in the history stack.
This means: If you want to register a receiver in the Onresume () method in the activity class, you should also unregister in the Activity Class OnPause () method. Do not unregister in the Onsaveinstancestate () method because the Broadcastreceiver will not be activated if the user exits the current activity (removing activity from the task).
Features: When the application is closed, no longer listens. For the app we're developing, the more power-saving it gets, the more popular the user is, so it's a good choice to register in code for receiver that doesn't have to be listening after the app exits.
2> registered in Androidmanifest.xml (static registration), the code is as follows:
1 <receiver android:name= ". Mybroadcastreceiver ">2 <intent-filter>3 <action android:name=" Com.example.administrator.broadcastreceiver.SEND_MESSAGE "/>4 </intent-filter>5 </receiver>
Feature: Listen regardless of whether the application is active or not. For example, an application listens for memory usage, and when it is installed on the phone, it listens to the memory state regardless of its state.
3.Broadcast Introduction
The 3.1Broadcast is divided into two types:
1>normal Broadcast (general broadcast):
The Normal broadcast is completely asynchronous and can be received (logically) by all receivers at the same time, with high efficiency of message delivery. The disadvantage is that the recipient cannot pass the result to the next recipient, and cannot prevent the propagation of the broadcast intent
2>ordered Broadcast (orderly broadcast):
Recipients of the Ordered broadcast receive broadcast in the order in which they are pre-declared priority. If Priority:a>b>c, then broadcast pass to A->b->c first.
3.2 Two ways to send a broadcast:
1>sendbroadcast (): Send normal broadcast
2>sendorderedbroadcast (): Send ordered broadcast
For ordered broadcast, the recipient is executed sequentially, based on the priority level declared by the recipient, and the recipient receiving the broadcast receives the priority to terminate broadcast ( Call Broadcastreceiver's Abortbroadcast () method), then the recipient will not be able to fetch the broadcast.
Not only that, for ordered broadcast, the recipient with high priority can save the result to broadcast through the Setresultextras bundle bundle, and the next recipient can pass the bundle bundle = Getresultextras (True) gets the previous data
TIP: The system receives SMS, the broadcast belongs to ordered broadcast. If you want to intercept a user receiving a text message, you can set a priority, let the custom Broadcastreceiver receive the SMS first, and then terminate the broadcast.
Android four components of the Broadcastreceiver