Android broadcast receiver (broadcast receiver)

Source: Internet
Author: User

First, Introduction

Broadcast receiver is a broadcast receiver that receives custom and system anchors. It can also be called a listener.

Broadcast intent, like intent, is the medium of communication, and unlike intent, broadcast intent is received by multiple components at the same time.

Broadcast intent broadcast mechanism, broadcast sources emit messages, and through AMS (Activity Manager service), multiple components can receive the same message. Paired with broadcast intent, the broadcast receiver broadcast receiver is used to receive broadcast intent broadcast messages.

Intent and broadcast Intent comparison:

(Figure from Android authoritative guide)

second, broadcast receiver registration method

1. Static Registration

The static registration is in the Androidmanifest.xml file, through the label <receiver> Register, using the tag <intentfilter> set intent of the message to be filtered. The life cycle of this registration method is the obliged application, which receives the message after the program finishes, and the program runs automatically and is executed by the receiver event.

1 <!--broadcast receive class name -2 <!--exported sets whether this broadcast receiver can receive broadcast messages from other applications -3 <!--permission is the right of this broadcast and can only receive broadcasts that meet the rights app -4 <receiver5     Android:name=". Timebroadcastreceiver "6 android:exported= "false"7 android:permission= "Com.example.RECEIVER_DEMO">8     <!--priority level for prioritized ordered broadcasts -9     <Intent-filterandroid:priority= "+">Ten         <!--Broadcast message name - One         <ActionAndroid:name= "Android.intent.action.ACTION_TIME_TICK" /> A     </Intent-filter> - </receiver>

(1). Exported is to set whether or not to receive broadcasts from other apps, the default value is set by whether there is Intent-filter, Intent-filter is true, and vice versa is false. When True, receives other app broadcast messages.

  (2). Android:permission is the broadcast permission that is set for broadcast only for apps that have requested this permission.

2. Dynamic Registration

Dynamic registration is using Registerreceiver (...) method to register, in which the broadcast life cycle is the same as the application, which ends at the end of the program. Also available through Unregisterreceiver (...) method to unregister broadcast.

1 //after the receiver receives the message, the handler functions callback handler2Mtimereceiver =NewTimebroadcastreceiver (mhandler);3Intentfilter Intentfilter =NewIntentfilter ();4 //Broadcast message name5 intentfilter.addaction (Intent.action_time_tick);6 intentfilter.addaction (intent.action_time_changed);7 //ordered broadcast priority level8Intentfilter.setpriority (100);9 //registering a broadcast classTenRegisterreceiver (Mtimereceiver, Intentfilter);

  3. Custom Permissions

1 <!--  -2<android:name = "Com.example.RECEIVER_DEMO"  />3<!----4<   android:name= "Com.example.RECEIVER_DEMO"/>

PS: The difference between the two registration methods is the life cycle of different, in the system, some broadcast messages can only use dynamic registration, such as: Listening time.

third, broadcast Receiver send mode

1. Normal (asynchronous) broadcast mode

Ordinary broadcast mode, also known as asynchronous broadcast, the message of the ordinary broadcast can be received by all recipients, the message delivery efficiency is high, but the disadvantage is that the results after receiving the message will not be passed to the next recipient, and can not abort broadcast intent broadcast.

1 New Intent (); 2 intent.setaction (Intent.action_time_tick); 3 intent.setaction (intent.action_time_changed); 4 Sendbroadcast (intent);

2. Orderly broadcast Mode

Ordered broadcasts receive broadcast messages sequentially by the priority level set by the recipient (the priority value range -1000~1000 between 1000 and highest). After receiving a broadcast message, you can add some data from this recipient to the next broadcast recipient in broadcast intent. However, the message delivery efficiency of this method is relatively low.

1 New Intent (); 2 // Message Name 3 intent.setaction (Intent.action_time_tick); 4 intent.setaction (intent.action_time_changed); 5 // receiverpermission Message Permissions 6 Sendorderedbroadcast (Intent, receiverpermission);

Summarize:

1. General broadcast: Asynchronous, high-efficiency message delivery, data can not be shared.

2. Orderly broadcast: synchronization, Message delivery efficiency is low, the data can be shared.

Iv. Receiver Implementation
1  Public classTimebroadcastreceiverextendsBroadcastreceiver2 {3     protected Static FinalString TAG = "Timebroadcastreceiver";4 5 @Override6      Public voidOnReceive (Context context, Intent Intent)7     {8         Switch(Intent.getaction ())9         {Ten              CaseIntent.action_time_tick: OneSimpleDateFormat formatter =NewSimpleDateFormat ("yyyy mm month DD Day HH:MM:SS"); ADate curdate =NewDate (System.currenttimemillis ()); -String str =Formatter.format (curdate); - log.d (TAG, str); the                 //after receiving the broadcast, abort the broadcast - abortbroadcast (); -                  Break; -         } +     } -}

Note: the OnReceive () method must be completed in 10 seconds and will throw "application No Response" If it is out of time. When OnReceive () takes a long time to execute, it needs to be implemented using a service (Context.startservice ()).

Android broadcast receiver (broadcast receiver)

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.