Android Broadcast Summary

Source: Internet
Author: User



1, Life cycle

In the official Android documentation, we recommend Registerreceiver in Onresume and Unregisterreceiver in OnPause. The reasons they give are:

If Registering a receiver in your activity.onresume () implementation, you should unregister it in Activity.on Pause (). (You won ' t receive intents when paused, and this would cut down on unnecessary system overhead). Do is unregister in activity.onsaveinstancestate (), because this won ' t is called if the user moves back in the history St Ack.



2, the difference between ordered broadcast and disordered broadcasting

Broadcast receivers (BROADCASTRECEIVER) are used to listen for system events or application events, and can send broadcast intentions to the system by calling Context.sendbroadcast (), Context.sendorderedbroadcast (). Broadcast an intent (Intent) can be received by multiple broadcast receivers, allowing you to react to events without altering the original application.


The Context.sendbroad () is primarily used to broadcast out-of-order events (also known as ordered broadcast Normal broadcast), where all receivers are theoretically receiving events at the same time. At the same time, this is a better approach to the efficiency of message delivery. The Context.sendorderedbroadcast () method is used to broadcast an ordered event (Ordered broadcast) to the system, and the recipient receives intent sequentially, in the order in which they are set in the Manifest.xml file. Sequential, received priority can be set in the System configuration file (declared in the android:priority attribute of the intent-filter element, the higher the value of the priority level, the value range is 1000 to 1000. Of course, you can also set the SetPriority () method that calls the Intentfilter object). For ordered broadcasts, the previous receiver is able to handle the received broadcast intent (Intent). The processing result is placed in the broadcast intent and then passed to the next recipient, and of course the preceding receiver has the right to terminate the broadcast further. Assuming the broadcast is terminated by the previous receiver, the receiver will no longer be able to receive the broadcast.


3, persistent broadcast Sendstickybroadcast

Sticky type of broadcast will save the last broadcast of intent, only you register to this broadcast, you can directly obtain the last intent. (until Removestickybroadcast the broadcast)

Perform a sendbroadcast (Intent) that was "sticky," meaning the Intent you were sending stays around after the broadcast was C Omplete, so-others can quickly retrieve that data through the return value of Registerreceiver (Broadcastreceiver, Int Entfilter). In all other ways, this behaves the same as Sendbroadcast (Intent).

You must hold the Broadcast_sticky permission in order to use this API. If you don't hold the that permission,securityexception'll be thrown.

It probably means that the broadcast will be stuck (waiting) so that someone will be able to receive the broadcast as soon as possible. Other functions are the same as sendbroadcast. However, using Sendstickybroadcast to send a broadcast requires BROADCAST_STICKY permission, assuming that no permission throws an exception.

and ordered types of broadcasts. The intent will not be saved, assuming that there is no intent at the time, then will not be available later.



4, Broadcast life cycle

Each time the broadcast arrives, the Broadcastreceiver object is created again, and the OnReceive () method is called, and the object is destroyed when it finishes running. When the OnReceive () method does not finish running within 10 seconds, Android will feel that the program is unresponsive.


















Android Broadcast Summary

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.