Android Service (ii) broadcastreceiver

Source: Internet
Author: User

Introduction of Broadcastreceiver

Broadcastreceiver, used for asynchronous reception of broadcast intent, broadcast intent by calling Context.sendbroadcast () send, Broadcastreceiver () receive.

The broadcast intent is implemented by calling Context.sendbroadcast (), Context.sendorderedbroadcast (), Context.sendstickybroadcast (). Usually a broadcast intent can be received by multiple broadcast receivers subscribing to this intent, and the broadcast receiver is similar to the topic message receiver in JMS.

Broadcast receivers can only receive broadcasts, respond to broadcast notifications, and many broadcasts are generated from system code, such as notification of time zone changes, low battery power, user-changed language preference, or power-on startup, etc.

Broadcast receivers do not have a user interface, but they can receive information to initiate an activity or use Notificationmanager to notify the user.

Broadcastreceiver Receive broadcast mode:

1. Normal broadcasts (regular broadcast), sent with Context.sendbroadcast () is completely asynchronous, and they all run in an undefined order, usually at the same time. This is more efficient, but it means that receiver cannot contain the results or aborted APIs that you want to use.

2. Ordered broadcasts (ordered broadcast), with Context.sendorderedbroadcast () sent each time sent to a receiver. The so-called order is that each receiver can be propagated to the next receiver after execution, or it can stop spreading completely--not to other receiver. The sequence of receiver runs can be controlled by the android:priority inside the matched Intent-filter, and priority in any order when the receiver priority is the same.

Second, Broadcastreceiver registration mode

1 Static Registration

Androidmanifest.xml, application inside, define the receiver and set the action to receive

<receiver android:name= ". Receiver. Musicreceiver " 

>     
    <intent-filter>     
        <action android:name=" Com.homer.receiver.musicReceiver "/>     
    </intent-filter>     
</receiver>

2 Dynamic Registration

In an activity, you need to call Registerreceiver () in OnStart () to register and invoke the Unregisterreceiver () release service in OnStop

Private Musicreceiver receiver;     
         
@Override 
protected void OnStart () {     
    super.onstart ();     
             
    Receiver = new Musicreceiver ();     
    Intentfilter filter = new Intentfilter ();     
    Filter.addaction ("Com.homer.receiver.musicReceiver");     
    This.registerreceiver (receiver, filter);     

@Override 
protected void OnStop () {     
    this.unregisterreceiver (receiver);     

    Super.onstop ();     
}

Comparison of 32 kinds of registration methods

Static registration mode, by the system to manage receiver, and the program in all receiver, can be in the XML inside a glance

Dynamic registration, hidden in the code, more difficult to find; Special attention is required to call the Context.unregisterreceiver () method before exiting the program. Generally in the activity of the onstart () inside to register, OnStop () inside for cancellation. The official reminder that if you register in Activity.onresume (), you must log off at Activity.onpause ().

Three, Broadcastreceiver life cycle

A Broadcastreceiver object is valid only if it is invoked onreceive (context, Intent), and when returned from the function, the object is invalid, ending the lifecycle.

Therefore, it can be seen from this feature that in the invoked OnReceive (context, Intent) function, there can be no too time-consuming operation, can not use the thread to execute. For time-consuming operations, it should be done in StartService. Because the broadcastreceiver may not be valid when the result returned by another asynchronous operation is obtained.

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.