What is Broadcastreceiver?
Broadcastreceiver, a broadcast receiver, is a system global listener that listens to the global broadcast message of the system, so it can easily communicate between system components.
Broadcastreceiver is a listener, but it is different from the onxxxlistener used before, those are just program-level listeners, running in the process of the specified program, and when the program exits, the Onxxxlistener listener closes. , but Broadcastreceiver is a system-level listener, and it has its own process, and as long as there is a matching broadcast being sent in intent form, Broadcastreceiver is activated.
Although the four major components of Android, Broadcastreceiver also has its own independent declaration cycle, but it is different from activity and service. When the system registers a broadcastreceiver, each time the system releases the broadcast in a intent form, the system creates a corresponding instance of the Broadcastreceiver broadcast receiver. and automatically triggers its onreceive () method, and when the OnReceive () method is executed, the Broadcastreceiver instance is destroyed. Although it enjoyed a separate process alone, it was not unrestricted, and if the Broadcastreceiver.onreceive () method could not be completed within 10 seconds, the Android system would assume that the Broadcastreceiver object was unresponsive. The ANR (Application No Response) dialog box is then ejected, so do not perform some time-consuming operations within the broadcastreceiver.onreceive () method.
If you need to do some time-consuming operations based on broadcast content, generally consider starting a service through intent to do this, rather than opening a new thread in Broadcastreceiver to complete the time-consuming operation. Because the life cycle of the broadcastreceiver itself is very short, what may happen is that the child thread has not yet finished, Broadcastreceiver has exited, and if Broadcastreceiver is at the end of the process, The thread is marked as an empty thread, and according to the Android memory management policy, when system memory is tight, the priority level is terminated, and the thread with the lower precedence is ended, and the empty thread is the lowest priority, which may cause the broadcastreceiver-initiated child thread to be unable to perform completion.
Types of Broadcastreceiver
As mentioned above, when the system sends a broadcast out in a intent form, all matching broadcastreceiver are instantiated, but there is a difference, depending on how the broadcast is transmitted, There are two kinds of broadcast in the system:
- Normal radio: Normal broadcase, which is completely asynchronous, that is, logically, when a broadcast is emitted, all matching broadcastreceiver receive broadcast at the same time. The advantage is that the transmission efficiency is higher, but also has the shortcoming, is one broadcastreceiver cannot affect other response this broadcast broadcastreceiver.
- Ordered broadcast: Ordered broadcast, which is synchronous execution, that is, the receiver of the ordered broadcast will receive the broadcast according to the priority of the advance declaration, is the chain structure, the higher the priority ( -1000~1000), the more executed first. Because it is sequential execution, all high priority receivers can pass the execution results to the next receiver, or terminate broadcast propagation (via the Abortbroadcast () method), once the broadcast propagation is terminated, A receiver that is less than its priority will no longer receive this broadcast.
Although the system has two kinds of broadcast, but the general system sends out the broadcast all is the orderly broadcast, therefore may through the priority control, before the system built-in program response, to broadcast in advance responds. This is the principle of the software for some interceptor classes in the market, such as SMS interceptors and phone interceptors.
How to send a broadcast
There are two different types of broadcast in the system, and the context provides different ways to publish them, depending on how broadcast is propagated:
- Sendbroadcast (): Send regular broadcast.
- Sendorderedbroadcast (): Send an ordered broadcast.
The above two methods have multiple overloaded methods, based on different scenarios, the simplest way is to pass a intent directly to send a broadcast.
How to use Broadcastreceiver
Broadcastreceiver is essentially a listener, so the method of using Broadcastreceiver is also very simple, just inherit broadcastreceiver and rewrite onreceive in it (context Context,intent Intent) can be. Once the broadcastreceiver is implemented and deployed to the system, it can be anywhere in the system, through Sendbroadcast, Sendorderedbroadcast method sends broadcast to this broadcastreceiver.
But just inheriting broadcastreceiver and implementing the OnReceive () method is not enough, the same Android system component, it must be registered in the Android system, there are two ways to register a broadcastreceiver:
- Use Content.registerreceiver (broadcastreceiver receiver, Intentfilter filter) in your code to register, Log off using the Content.unregisterreceiver (Broadcastreceiver Receiver) method when you are finished using.
- Register using the manifest file Androidmanifest.xml, register with <receiver/> node in <application/> node, and use Android: Specify the registered Broadcastreceiver object in the name attribute, typically through <Intent-filter/> specify <action/> and <category/>, and in < The priority of the Broadcastreceiver is set by the Android:priority property in the Intent-filter/> node, and the higher the value in the -1000~1000 range.
Although the Android system provides two ways to register broadcastreceiver, it is generally used in actual development to register with the manifest file:
1 <receiver android:name= "Cn.bgxt.Broadcastdemo.Basic.BasicBroadcast" >
2 <intent-filter android:priority= >
3 <action android:name= "Cn.bgxt.Broadcastdemo.Basic.broadcast"/>
4 </intent-filter>
5 </receiver>
The following is a simple example that explains the Broadcastreceiver declaration and how to send a message to this broadcastreceiver.
First, declare a broadcastreceiver,basicbroadcast.java:
1 package cn.bgxt.Broadcastdemo.Basic;
2
3 import android.content.BroadcastReceiver;
4 Import Android.content.Context;
5 import android.content.Intent;
6 Import Android.widget.Toast;
7
8 public class Basicbroadcast extends Broadcastreceiver {
9
@Override one public void OnReceive (context, Intent Intent) { toast.maketext (context) received broadcast, the message is: "+ Intent.getstringextra ("msg"), toast.length_short). Show (); }
16}
Then declare an activity for sending Broadcast:BasicActivity.java:
1 package cn.bgxt.Broadcastdemo.Basic;
2 3 Import COM.BGXT.DATATIMEPICKERDEMO.R;
4 5 Import android.app.Activity;
6 Import android.content.Intent;
7 Import Android.os.Bundle;
8 Import Android.view.View;
9 Import Android.widget.Button; A public class Basicactivity extends activity {Button Btnbasicsendnormal, btnbasicsendordered @Ov Erride protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 17
Setcontentview (R.layout.activity_basic);
Btnbasicsendnormal = (Button) Findviewbyid (r.id.btnbasicsendnormal);
btnbasicsendordered = (Button) Findviewbyid (r.id.btnbasicsendordered); Btnbasicsendnormal.setonclicklistener (New View.onclicklistener () {@Override 24 public void OnClick (View v) {Intent broadcast=new Intent (); Broadcast.setaction ("cn . Bgxt.
Broadcastdemo.Basic.broadcast "); 27 Broadcast.putextra ("msg", "This is an ordinary broadcast");
Sendbroadcast (broadcast);
29} 30}); Btnbasicsendordered.setonclicklistener (New View.onclicklistener () @Override 35 public void OnClick (View v) {Intent broadcast=new Intent (); broadcast.setactio
N ("Cn.bgxt.Broadcastdemo.Basic.broadcast");
Broadcast.putextra ("msg", "This is an orderly broadcast");
Sendorderedbroadcast (broadcast, NULL);
40} 41}); }
Effect Display: