Android Broadcast Summary

Source: Internet
Author: User



1, Life cycle

In the official Android documentation, we recommend that we do registerreceiver in Onresume, Unregisterreceiver in OnPause, and 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 modifying the original application.


where Context.sendbroad () is primarily used to broadcast unordered events (also known as ordered broadcast Normal broadcast), which means that all receivers are theoretically receiving events at the same time, while executing, which is a good practice for the efficiency of message delivery. The Context.sendorderedbroadcast () method is used to broadcast ordered events (Ordered broadcast) to the system, The recipient receives the intent in order, sequentially, in the order in which they are set in the Manifest.xml file, and the received priority can be set in the System configuration file (declared in the android:priority attribute of the intent-filter element). The larger the value, the higher the priority, with a value ranging from 1000 to 1000. Of course, you can also set the SetPriority () method of calling the Intentfilter object). For an ordered broadcast, the previous receiver can process the received broadcast intent (Intent), place the processing result in the broadcast intent, and pass it on to the next recipient, and of course the preceding receiver has the right to terminate the broadcast further. If 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 intent, as long as 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 can register the broadcast and receive the broadcast as soon as possible. Other features are the same as sendbroadcast. However, using Sendstickybroadcast to send a broadcast requires Broadcast_sticky permission, which throws an exception if there is no such permission.

and orderly type of broadcast, then will not save intent, if not get intent, then will not be.



4, Broadcast life cycle

Each time the broadcast arrives, the Broadcastreceiver object is recreated, and the OnReceive () method is called, and the object is destroyed when it finishes executing. When the OnReceive () method is not completed within 10 seconds, Android will consider the program unresponsive.


















Android Broadcast Summary

Related Article

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.