1th section Broadcast Receiver Overview
Many times, we want an application to be able to perceive certain changes in the state of the system when it is not running, and let the application run if the conditions are right.
For example, an application to intercept the harassment of the phone, usually it may not need to run, it is only concerned about when the phone comes, to see if this number is the number of harassment calls, if it is to send a hint to the user, if not, it is not related to it, do nothing.
In order to achieve this kind of function, the Android system introduced one of the four components BroadcastReceiver , designed a set of broadcasting and reception mechanism,
- The components of any application or Android can send a broadcast message to the system like radio waves
Broadcast ;
- The information is disseminated in the system according to certain rules (the rules of dissemination will be described in detail later);
- Any app or Android's own components have one
接收机 -- Broadcast Receiver This 接收机 will tell the Android system that it can receive a certain kind of broadcast;
- If the Android system identifies the broadcast that is being propagated in the current system,
接收机 it is a broadcast of concern, so it will 接收机 run and let it 接收机 decide what to do next. Whether to let the 接收机 other component (service or Activity ) continue to respond further, or ignore the broadcast.
2nd section customizing Broadcast Receiver
They are defined Broadcast Receiver in the same way, whether they are applications or system components.
Broadcast ReceiverThere are two ways of defining
Static definition: will be Broadcast Receiver declared to the AndroidManifest.xml configuration file, it is characterized by the application does not need to run, the system can be described by the configuration file to determine whether the current system of broadcasting is the receiver concerned;
Dynamic definition: In code dynamic generation Broadcast Receiver , it is characterized by the application to run, in the process of running to determine whether the current system of broadcasting is the receiver concerned;
2.1 Static Broadcast Receiver
Let's start by looking at how to define a static Broadcast Receiver .
Inherits BroadcastReceiver the class, implements its onReceive interface,
publicclass MyReceiver extends BroadcastReceiver { publicMyReceiver() { } //实现onReceive接口,当收到指定的广播后被触发 @Override publicvoidonReceive(Context context, Intent intent) { //添加对应的逻辑处理 }}
In the AndroidManifest.xml configuration file, the declaration is Broadcast Receiver created,
<receiverandroid:name=". Myreceiver "android:enabled=" true "android:exported=" true "> <intent-filter> <action android:name="Custom.action.mybroadcast"/>---designated broadcast for attention<category android:name="Android.intent.category.DEFAULT" /> </intent-filter></receiver>
The intent-filter value of the property set in the label here android:name is the custom.action.mybroadcast 接收机 broadcast of interest.
When a custom.action.mybroadcast broadcast called is propagated in the system, it is MyReceiver run and triggers its onReceive() function. The developer will need to add the corresponding logical processing here.
2.2 Dynamic Broadcast Receiver
When the app is running, AndroidManifest.xml it doesn't matter if it's not declared in the file, Broadcast Receiver it can be created dynamically,
-
is similar to creating a static broadcast Receiver , first inheriting the Broadcastreceiver class, implementing its onreceive interface ,
public class myreceiver extends broadcastreceiver { public myreceiver () {} //implements OnReceive interface, triggered when a specified broadcast is received @Override public void OnReceive (Context context, Intent Intent) {//add corresponding logic processing }}
Register Broadcast Receiver , and tell the system Broadcast Receiver which radio to focus on, can add a variety of concerned broadcasts,
//设置过滤器,确定关心的广播new IntentFilter();filter.addAction("custom.action.mybroadcast");//可以添加多种关心的广播filter.addAction(......);//创建Receivernew MyReceiver();//注册到系统当中,完成Receiver的设置registerReceiver(receiver, filter);
After use, be sure to log off Broadcast Receiver , or the system will give a warning prompt.
unregisterReceiver(receiver);
Dynamic Broadcast Receiver After the registration is completed must be logged off, otherwise it will occupy the system resources, wasting resources.
/*******************************************************************/
* Copyright Notice
* This tutorial is only published in CSDN and the green Bean Network, other websites appear this tutorial is infringing.
/*******************************************************************/
3rd section send broadcast
Any application or component of the Android system can send broadcast messages to the system, Broadcast as long as Context the sendBroadcast() method is used,
new Intent("custom.action.mybroadcast");sendBroadcast(i);
The broadcast can be divided into two categories, one is out-of-order broadcasting, the other is ordered broadcast.
3.1 Shuffle broadcasts
This is a broadcast that does not need to consider the receiver order, for example, there are 3 接收机 , all concerned about custom.action.mybroadcast this broadcast, it doesn't matter who received the first person received. 接收机no other 接收机 access to this broadcast can be prevented.
3.1.1 Send
Send out-of-order broadcasts, as long as Context you use the sendBroadcast() method,
new Intent("custom.action.mybroadcast");sendBroadcast(i);
3.1.2 Receive
AndroidManifest.xmlwhen declaring static in Broadcast Receiver ,
<receiverandroid:name=". Myreceiver "android:enabled=" true "android:exported=" true "> <intent-filter> <action android:name="Custom.action.mybroadcast"/> <category android:name="Android.intent.category.DEFAULT" /> </intent-filter></receiver>
As described earlier, there is no special handling.
3.2 Orderly broadcast
This is a broadcast that needs to take into account the order of receiver reception, for example, 3 of 接收机 them are concerned with custom.action.mybroadcast this broadcast, then the Android system will be based on the 接收机 priority of the 3 statements broadcast delivery.
and orderly broadcasts can be intercepted.
For example, a broadcast is delivered in order to 3 接收机 -a B C, but B intercepts the broadcast, so C will not receive this broadcast.
3.2.1 Send
new Intent("custom.action.mybroadcast"null);
3.2.1 Receive
When receiving, you need to intent-filter set a property for the tag android:priority , indicating the 接收机 priority of this. The priority is from 1000 to 1000, and the higher the value, the greater the priority level.
<receiverandroid:name=". Myreceiver "android:enabled=" true "android:exported=" true "> <intent-filter android:priority="+"> <action android:name="Custom.action.mybroadcast"/> <category android:name="Android.intent.category.DEFAULT" /> </intent-filter></receiver>
After receiving the broadcast, Broadcast Receiver you can intercept the broadcast, prohibit it from spreading down,
publicclass MyReceiver extends BroadcastReceiver { publicMyReceiver() { } //实现onReceive接口 @Override publicvoidonReceive(Context context, Intent intent) { //禁止往下传播 abortBroadcast(); }}
If 接收机1 in onReceive() , want to pass the data to 接收机2 the next,
接收机1You can use setResultExtras() the method,
publicclass MyReceiver1 extends BroadcastReceiver { publicMyReceiver() { } @Override publicvoidonReceive(Context context, Intent intent) { new Bundle(); b.putString("data""this data from MyReceiver"); setResultExtras(b); }}
In the 接收机2 ,
public class myreceiver2 extends broadcastreceiver { public myreceiver2 () {} @Override public void onreceive (context context, Intent Intent) {Bundle b = getresu Ltextras (true ); if (B!=null ) {//data is the previous receiver 1-this data from myreceiver String data = B.get String ( "data" ); } }}
If you want to put the data into onReceive() the incoming Intent , it will not be delivered successfully,
@OverridepublicvoidonReceive(Context context, Intent intent) { //这是不会成功的 intent.putExtra("data""this data from MyReceiver");}
Pass the data, be sure to pass BroadcastReceiver the setResultExtras() method provided.
In addition, the test found that, as long as the data is not passed between the ordered broadcast, the use sendBroadcast() of the method can also successfully send an orderly broadcast.
new Intent("custom.action.mybroadcast");sendBroadcast(i);
Broadcastreceiver of four components (i)-Customizing "Radio" and sending "broadcast"