Introduction to Android common components Broadcast, androidbroadcast
I. Introduction to Broadcast
Broadcast is one of the four main components of Android. It can be divided:
1. When a normal broadcast sends a broadcast, all the broadcast recipients that listen on the broadcast can listen to the broadcast.
2. After asynchronous broadcast processing, Intent still exists. At this time, registerReceiver (BroadcastReceiver, IntentFilter) can still receive its value until you remove it, you cannot pass the processing result to the next receiver or terminate the broadcast.
3. Ordered broadcast receives the broadcast in the order of priority of the receiver. The priority is not stated in priority in intent-filter. The value ranges from-1000 to 1000. The higher the value, the higher the priority. the continuation of broadcast intent can be terminated. the recipient can tamper with the content.
Ii. Broadcast implementation process
1. Registration
A. configure file registration
1 <! -- Register a custom static broadcast receiver --> 2 <receiver er android: name = "com. example. androidtest. broadcast. mycycler "> 3 <intent-filter> 4 <action android: name =" MyAction "/> 5 </intent-filter> 6 </Cycler>
B. Code registration
1 MyReceiver myReceiver = new MyReceiver();2 IntentFilter filter = new IntentFilter();3 filter.addAction("MyAction");4 registerReceiver(myReceiver,filter);
2. Send Broadcast
1 Intent intent = new Intent (); 2 intent. setAction ("MyAction"); 3 intent. putExtra ("msg", "this is the message sent by the broadcast"); 4 sendBroadcast (intent );
3. Implementation of the receiving Class
Inherit BroadcastReceiver and override the onReceive method.
1 public class MyReceiver extends BroadcastReceiver2 {3 public void onReceive (Context context, Intent intent) 4 {5 Toast. makeText (context, "broadcast message is:" + intent. getStringExtra ("msg"), Toast. LENGTH_SHORT ). show (); 6} 7 8}
3. How to implement an internal class as a broadcast receiving class?
1. Registration
A. configure file registration
1 <! -- Register a custom static broadcast receiver --> 2 <receiver er android: name = "com. example. androidtest. broadcast. BroadcastTest $ mycaster Er"> <! -- Note internal class writing: Common class $ internal class --> 3 <intent-filter> 4 <action android: name = "MyAction"/> 5 </intent-filter> 6 </Cycler>
B. Code registration
1 MyReceiver myReceiver = new MyReceiver();2 IntentFilter filter = new IntentFilter();3 filter.addAction("MyAction");4 registerReceiver(myReceiver,filter);
2. Send Broadcast
1 Intent intent = new Intent (); 2 intent. setAction ("MyAction"); 3 intent. putExtra ("msg", "this is the message sent by the broadcast"); 4 sendBroadcast (intent );
3. Implementation of the receiving Class
Note: if it is registered in the configuration file, the class must be declared as static; otherwise, it cannot be found. If static is omitted in the Code Registration Section
1 public static class MyReceiver extends BroadcastReceiver 2 {3 public void onReceive (Context context, Intent intent) 4 {5 Toast. makeText (context, "broadcast message is:" + intent. getStringExtra ("msg"), Toast. LENGTH_SHORT ). show (); 6} 7 8}
Iv. Purpose
Boot, screen lock, low power ......
5. Note
The life cycle is only about 10 seconds. If you do more than 10 seconds in onReceive (), an error is reported.
Each time the broadcast arrives, the BroadcastReceiver object is re-created and the onReceive () method is called. After execution, the object is destroyed. when the onReceive () method is not completed within 10 seconds, Android considers the program to be unresponsive. therefore, you cannot perform some time-consuming operations in BroadcastReceiver. If not, the ANR (Application NoResponse) dialog box is displayed.
Partial content taken from: http://yangguangfu.iteye.com/blog/1063732