Original URL: http://www.cnblogs.com/plokmju/p/android_broadcastreceiver.html
Objective
Android four components, Activity, Service, ContentProvider, Broadcastreceiver, in addition to Broadcastreceiver, and other in the previous blog has been explained. Well, this blog is mainly about broadcastreceiver content.
This article mainly involves the following content:
- What is Broadcastreceiver
- Types of Broadcastreceiver
- How to send a broadcast
- How to use Broadcastreceiver
What is Broadcastreceiver?
Broadcastreceiver, the broadcast receiver, is a system-wide listener that listens to the system's global broadcast messages, so it can easily communicate between system components.
Broadcastreceiver is a listener, but unlike the previous onxxxlistener, those are just program-level listeners that run in the process of the specified program, and when the program exits, the Onxxxlistener listener shuts down. , but Broadcastreceiver belongs to the system level listener, it has its own process, as long as there is a matching broadcast is sent in the form of intent, broadcastreceiver will be activated.
Although the four main components of Android, Broadcastreceiver also has its own independent declaration cycle, but with the activity, service is different. When the system registers a broadcastreceiver, each time the system publishes 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. While it alone enjoys a single process, it is not unlimited, and if the Broadcastreceiver.onreceive () method cannot be completed within 10 seconds, the Android system will assume that the Broadcastreceiver object is 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 the broadcast content, you generally consider starting a service through intent to do this, instead of opening a new thread in Broadcastreceiver to complete the time-consuming operation. Because the life cycle of the broadcastreceiver itself is very short, it is possible that the child thread has not ended and Broadcastreceiver has exited, and if the broadcastreceiver process is over, The thread will be marked as an empty thread, according to the Android memory management policy, when the system memory is tight, the priority is the end of the thread, and the empty thread is the lowest priority, which may cause the broadcastreceiver-initiated child thread to fail to complete.
Types of Broadcastreceiver
As mentioned above, when the system sends a broadcast out in a intent form, all matching broadcastreceiver will be instantiated, but there is a difference, depending on the way broadcast is propagated, There are two types of broadcast in the system:
- Normal broadcast: Normal broadcase, it 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 transfer efficiency is relatively high, but there are shortcomings, that is, a broadcastreceiver can not affect the other response to this broadcast broadcastreceiver.
- Orderly broadcast: Ordered broadcast, it is synchronous execution, that is, the receiver of the ordered broadcast will be in accordance with the pre-declared priority order to accept broadcast, is a chain structure, the higher the priority ( -1000~1000), the more first executed. Because it is sequential execution, all high-priority receivers can pass the execution result into the next sink, or terminate the propagation of broadcast (via the Abortbroadcast () method), once the broadcast propagation is terminated, A receiver with a priority below it will no longer receive this broadcast.
Although the system exists two types of broadcast, but the general system sends out the broadcast are orderly broadcast, so you can pass the priority control, before the system built-in program response, the broadcast in advance response. This is the principle of the software of some interceptor classes (such as SMS Blocker, phone blocker) on the market.
How to send a broadcast
There are two different broadcast in the system, and depending on how the broadcast is propagated, the context provides different ways to publish them:
- Sendbroadcast (): Sends a normal broadcast.
- Sendorderedbroadcast (): Sends an ordered broadcast.
The above two methods have multiple overloaded methods, depending on the scene use, the simplest is to pass a intent directly to send a broadcast.
How to use Broadcastreceiver
Broadcastreceiver is essentially a listener, so the way to use Broadcastreceiver is very simple, just inherit Broadcastreceiver, where you override OnReceive (Context Context,intent Intent). Once the broadcastreceiver is implemented and deployed to the system, it can be located anywhere in the system via Sendbroadcast, The Sendorderedbroadcast method sends broadcast to this broadcastreceiver.
But simply inheriting the broadcastreceiver and implementing the OnReceive () method is not enough, as is the Android system component, which must also be registered with the Android system, registering a broadcastreceiver in one of two ways:
- Register using Content.registerreceiver (broadcastreceiver receiver, Intentfilter filter) in your code, Use the Content.unregisterreceiver (Broadcastreceiver Receiver) method to log off after use.
- Register with the manifest file Androidmanifest.xml, in the <application/> node, register with the <receiver/> node and use Android: The Name property specifies the registered Broadcastreceiver object, which is typically also specified by <Intent-filter/> <action/> and <category/>, and in the < The priority of the Broadcastreceiver is set in the Intent-filter/> node through the Android:priority property, and in the -1000~1000 range, the value is higher than the priority level.
Although the Android system provides two ways to register broadcastreceiver, it is generally used in real-world development, or it is registered with a 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>
Here's 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 @Override11 public void OnReceive (Context Co ntext, Intent Intent) { toast.maketext (context,13) received broadcast with the message: "+ Intent.getstringextra (" msg "), 14 toast.length_short). Show (); }16}
Then declare an activity to send the 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;10 One public class Basicactivity extends Activity {Button btnbasicsendnormal, Btnbas icsendordered;13 @Override15 protected void onCreate (Bundle savedinstancestate) {super.oncreate (save Dinstancestate); Setcontentview (r.layout.activity_basic); btnbasicsendnormal = (Button) findviewbyi D (r.id.btnbasicsendnormal); btnbasicsendordered = (Button) Findviewbyid (r.id.btnbasicsendordered); btn Basicsendnormal.setonclicklistener (New View.onclicklistener () {@Override24 public void oncli CK (View v) {Intent broadcast=new Intent (); Broadcast.setaction ("Cn.bgxt.Broadcastdemo . Basic.broadcast "); Broadcast.putextra (" MSG "), "This is an ordinary broadcast"); Sendbroadcast (broadcast);}30}); btnbasicsendordered. Setonclicklistener (New View.onclicklistener () {@Override35 public void OnClick (View v) {36 Intent broadcast=new Intent (); PNs broadcast.setaction ("Cn.bgxt.Broadcastdemo.Basic.broadcast "); Broadcast.putextra (" msg "," This is an orderly broadcast "); Sendorderedbroadcast (broadcast, NULL); 40 }41}); 42}43}
Effect Show:
SOURCE download
Summarize
This blog has briefly explained the use of broadcastreceiver and the areas that need attention. However, in general practical projects, rarely custom broadcast to use, most of the cases are to intercept the system released broadcast, for the system published broadcast content to do the corresponding operation. Android system natively defines a lot of broadcast content, such as: power-on, shutdown, call, SMS, and so on some system state changes broadcast. However, about the system broadcast content, not in this blog, and then slowly detailed introduction.
"Turn" android--broadcast Broadcastreceiver