BroadcastReceiver of four android Components
Introduction to BroadcastReceiver
BroadcastReceiver is used to receive the Broadcast Intent sent by a program (including a user-developed program and a program in the system ).
Usage
Only two steps are required for the program to start BroadcastReceiver.
1. Create the Intent of the BroadcastReceiver to be started.
2. Call the sendBroadcast () or sendOrderedBroadcast () method of Context to start the specified BroadcastReceiver.
Private void sendBroadcast () {Intent intent = new Intent (); // set the channel com. aggreger. test intent. setAction (com. aggreger. test); intent. putExtra (msg, simple message); sendBroadcast (intent );}
The program receives Broadcast and uses BroadcastReceiver.
BroadcastReceiver is essentially a system-level listener with its own processes. As long as there is a matching Intent, it will be broadcast. You only need to override the onReceive () method of BroadcastReceiver, and specify the matching Intent. There are two ways to specify the matching intent;
Note: after each system Broadcast event, the BroadcastReceiver instance will be destroyed after the onReceive () method is executed. Therefore, @ Override protected void onDestroy () method (1) AndroidManifest is required. xml file
(2) java code
Private MyReceiver myReceiver; // set the channel in the code, and unbind Myer ER = new myReceiver (); IntentFilter filter = new IntentFilter (com. aggreger. test); registerReceiver (myReceiver, filter); @ Override protected void onDestroy () {super. onDestroy (); // unbind unregisterReceiver (mycycler );}
When an application sends a Broadcast Intent, all BroadcastReceiver matching the Intent may be started. The following program is used to receive broadcasts. After receiving the Broadcast, the Toast prompt is used.
Public class MyReceiver extends BroadcastReceiver {// used to receive the broadcast and use the Toast prompt @ Override public void onReceive (Context context, Intent intent) {Toast. makeText (context, receives the message, Toast. LENGTH_LONG ). show ();}}
The effect is as follows:
The following uses BroadcastReceiver to implement the alarm function
Private AlarmManager mAlarmManager; mAlarmManager = (AlarmManager) getSystemService (Context. ALARM_SERVICE); private void startAlarm () {Intent intent2 = new Intent (); // sets the channel intent2.setAction (com. aggreger. test); // 0x23 indicates the flag PendingIntent pendingIntent = PendingIntent. getBroadcast (getApplicationContext (), 0x23, intent2, PendingIntent. FLAG_UPDATE_CURRENT); mAlarmManager. setRepeating (AlarmManager. RTC_WAKEUP, System. currentTimeMillis () +, pendingIntent);} private void cancelAlarm () {Intent intent2 = new Intent (); // set the channel intent2.setAction (com. aggreger. test); PendingIntent pendingIntent = PendingIntent. getBroadcast (getApplicationContext (), 0x23, intent2, PendingIntent. FLAG_UPDATE_CURRENT); // enable the alarm in five seconds, and enable the alarm again in three seconds. setRepeating (AlarmManager. RTC_WAKEUP, System. currentTimeMillis () + 5000,3000, pendingIntent); mAlarmManager. cancel (pendingIntent );}
The effect is as follows:
Use System broadcast
One of the following monitors the network status and the other broadcasts when the program is uninstalled.