About four Android components: BroardcastReceiver

Source: Internet
Author: User

BroardcastReceiverIntroduction

Broadcast receiver (BroadcastReceiver) is one of the four major Android components used to receive broadcast Intent. The sending of broadcast Intent is achieved by calling Context. sendBroadcast () and Context. sendOrderedBroadcast. Generally, a broadcast Intent can be received by multiple broadcast recipients subscribed to this Intent.

Broadcast is a widely used mechanism for transmitting information between applications. BroadcastReceiver is a component that filters, receives, and responds to sent broadcasts. Your application can use it to filter external events and only receive and respond to external events that interest you (for example, when calls are made, or when data networks are available. The broadcast receiver has no user interface. However, they can start an activity or serice to respond to the information they receive, or use icationicationmanager to notify users. Notifications can be used in many ways to attract users' attention-flashing back lights, vibrations, and playing sounds. In general, a persistent icon is placed on the status bar. You can open it and get the message.

BroadcastReceiver itself does not implement a graphical user interface, but when it receives a notification, BroadcastReceiver can start the Activity as a response, notify the user through icationicationmananger, or start the Service.


BroadcastReceiverUse

To listen to a broadcast Intent, follow these steps:

1. Write a class that inherits BroadCastReceiver and override the onReceive () method. The broadcast receiver is only active when it executes this method. When onReceive () is returned, it is inactive. Note: To ensure smooth user interaction, some time-consuming operations should be placed in the thread, such as the class name SMSBroadcastReceiver.

Public class SMSBroadcastReceiver extends BroadcastReceiver {// action name String SMS_RECEIVED = "android. provider. telephony. SMS_RECEIVED "; public void onReceive (Context context, Intent intent) {if (intent. getAction (). equals (SMS_RECEIVED) {// processing: region change, insufficient power, incoming call }}}

2. register the broadcast receiver. Two methods are available for Dynamic Registration of the program and static registration in the AndroidManifest file (which can be understood as registration in the system, the following priority indicates the broadcast receiving level:

< receiver android:name = ".SMSBroadcastReceiver" >           < intent-filter android:priority = "1000" >< action android:name = " android.provider.Telephony.SMS_RECEIVED" />           
        
  < uses-permission android:name = "android.permission.RECEIVE_SMS" />< uses-permission android:name = "android.permission.SEND_SMS" /> 

Program Dynamic Registration, registered broadcast, the following priority indicates that the broadcast receiving level "2147483647" is the highest priority

IntentFilter intentFilter = new IntentFilter( "android.provider.Telephony.SMS_RECEIVED " );registerReceiver(smsBroadcastReceiver , intentFilter);

3. log out

UnregisterReceiver (smsBroadcastReceiver) can be completed in one sentence. We recommend that you log out in onPause or onDestroy.


There are three types of broadcast:

Normal Broadcast,Sent by Context. sendBroadcast (Intent myIntent)

Ordered Broadcast,Use Context. sent by sendOrderedBroadcast (intent, receiverPermission). The 2nd parameters of this method determine the broadcast level. The value range is-1000 to 1000. The higher the value, the higher the sending priority; the level at which the broadcast receiver receives the broadcast (the highest priority can be set through the priority in intentfilter to 2147483647). The same level of receipt is random, when the broadcast is received at a lower level, if the broadcast is received at a higher level or at the same level first, the broadcast can be intercepted by the abortBroadcast () method to prevent other receivers from receiving the broadcast. There are other constructor functions.

Asynchronous Broadcast,Use Context. sendStickyBroadcast (Intent myIntent) is also sent by sendStickyOrderedBroadcast (intent, resultReceiver, schedcast, initialCode, initialData, and initialExtras). This method has the ordered broadcast and asynchronous broadcast features; to send an asynchronous broadcast: "Android. permission. BROADCAST_STICKY"/> Permission. After receiving and processing the Intent, the broadcast still exists until you call removeStickyBroadcast (intent) to remove it.

Note: The intent parameter when sending the broadcast and Contex. startActivity () starts up with a different Intent. The former can be called by multiple broadcast receivers that subscribe to it, and the latter can only be called by one (Activity or service ).


BroadcastReceiverLifecycle

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

BroadcastReceiver does not support time-consuming operations. The ANR (Application No

Response) dialog box.

If you need to complete a time-consuming task, you should send Intent to the Service, which is done by the Service. subthreads cannot be used here, because the life cycle of BroadcastReceiver is very short, and the subthread may not end yet, And BroadcastReceiver will end first. once BroadcastReceiver ends, the process where BroadcastReceiver is located is easily killed first when the system requires memory because it is a blank process (process without any active components ). if its host process is killed, the working sub-thread is also killed. therefore, using sub-threads is not reliable.

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.