Ii. BroadcastReciver (broadcast receiver) 1. Description BroadcastReciver is located in the android. content package and is mainly used to filter and respond to broadcast messages (Intent. BroadcastReceiver itself does not implement a graphical user interface, but when it receives a broadcast message, BroadcastReceiver can start the Activity as a response or start the Service. 2. Running principle when other applications send broadcast messages, all applications registered with BroadcastReciver will check whether the registered IntentFilter matches the broadcast message, the BroadcastReciver onReceive () method is called for processing. Therefore, the BroadcastReciver application is developed mainly to implement the onReceive () method. 3. The life cycle is not as complex as the Activity. Generally, the life cycle is: start (called by other programs, receive the matched broadcast) --> Run (execute the onReceive () method) --> end (release the resource). Each time the broadcast arrives, the BroadcastReceiver object is re-created and onReceive () is called () method. After the 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. 4. There are two types of broadcast: a. Normal broadcast is completely asynchronous for multiple receivers. Generally, each receiver can receive the broadcast without waiting, the receiver does not affect each other. For such a broadcast, the receiver cannot terminate the broadcast, that is, it cannot prevent the receiving actions of other receivers. B. Ordered broadcast ordered broadcast is special. It is only sent to the receiver with higher priority each time, and then transmitted to the receiver with lower priority by the receiver with higher priority, the receiver with a higher priority has the ability to terminate the broadcast. 5. send and receive the broadcast. a. Send the broadcast. Construct an Intent object and call the sendBroadcast (Intent) method to send the broadcast. In addition, there are sendOrderedBroadcast (), sendStickyBroadcast (), and other methods B. Receive broadcast: this is achieved by defining an inherited BroadcastReceiver class, inheriting this class and overwriting its onReceiver method, in response to the event 6 in this method, after the BroadcastReceiver is created for broadcast registration, it cannot be put into working state. We need to register a specified broadcast address for it, otherwise, the following two types of registration are available for receiving broadcasts: a. Static registration 1 <receiver er android: name = ". mycycler "> 2 <intent-filter> 3 <action android: name =" android. intent. action. MY_BROADCAST "/> 4 <category android: name =" android. intent. category. DEFAULT "/> 5 </intent-fi Lter> 6 </receiver> B. dynamically register 1 MyReceiver receiver = new MyReceiver (); 2 3 IntentFilter filter = new IntentFilter (); 4 filter. addAction ("android. intent. action. MY_BROADCAST "); 5 6 registerReceiver (receiver, filter); the difference between the two registration methods is that static registration is resident memory, and applications can also receive broadcasts if they are disabled, dynamic registration is based on the life cycle of the program. Therefore, when the program exits, if it is not removed, the system will report an exception, prompting whether we forget to cancel the registration. Add the unregister code: 1 @ Override2 protected void onDestroy () {3 super. onDestroy (); 4 unregisterReceiver (receiver); 5} 7. Run the instance to write an example. When running the program, broadcast is sent, and the background log prints the message. MyReceiver code: Copy code 1 public class MyReceiver extends BroadcastReceiver {2 3 private static final String TAG = "MyReceiver"; 4 5 @ Override 6 public void onReceive (Context arg0, Intent intent) {7 // TODO Auto-generated method stub 8 String msg = intent. getStringExtra ("msg"); 9 Log. I (TAG, msg); 10} 11 12} copy the code Activity to send the broadcast code: 1 Intent intent = new Intent ("android. intent. action. MY_BROADCAST "); 2 inten T. putExtra ("msg", "Hello Receiver! "); 3 sendBroadcast (intent); use static registration broadcast directly. The broadcast in the above example is written by ourselves. Android provides many rich Native Broadcast addresses. Often used: boot: <action android: name = "android. intent. action. BOOT_COMPLETED "/> network connection: <action android: name =" android.net. conn. CONNECTIVITY_CHANGE "/> power change: <action android: name =" android. intent. action. BATTERY_CHANGED "/> time change: <action android: name =" android. intent. action. DATE_CHANGED "/> you only need to change the custom aggreger to trigger some corresponding events.