BroadcastReceiver of four android Components

Source: Internet
Author: User

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.


      
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.