The Broadcastreceiver of Android development

Source: Internet
Author: User

Create a broadcast receiver: Simply new class inherits from Broadcastreceiver, then overrides OnReceive method

There are two ways to register a broadcast:

1. Static registration: That is, register in the manifest file, for example, the following listen to call broadcast:

<receiver android:name= ". Mybroadcastreceiver ">            <intent-filter>                <action android:name=" android.intent.action.NEW_ Outgoing_call "/>            </intent-filter></receiver>

1. Dynamic registration: That is, registering in the code, for example:

New= "Android.intent.action.NEW_OUTGOING_CALL"new  intentfilter (action); Registerreceiver (receiver,intentfilter);

Dynamically registered broadcasts need to be de-registered in the OnDestroy method

Unregisterreceiver (receiver);

Note: Dynamically registered broadcasts rely on the lifecycle of the component that registers the broadcast

The static registration of the broadcast once registered, there has been

The type of broadcast is consistent with the type that the broadcast recipient configures in the manifest file, or the broadcast will not be received

Broadcast divided into ordered broadcast and disorderly broadcast

Out-of-order broadcasts: sent through Sendbroadcast (intent), cannot be intercepted, all broadcast receivers can be received at almost the same time, with no sequence to speak

Orderly broadcast: Sent through Sendorderedbroadcast (Intent,null), after the broadcast, only one broadcast receiver can be received at the same time, the execution is complete, the broadcast can continue to pass

A high-priority broadcast receiver receives broadcasts first, and can also use Abortbroadcast () to terminate the broadcast.


The priority can be set by the android:priority= "" parameter, the range is -1000~1000

If the priority of the two broadcast receivers is the same, the component registered first receives the broadcast

If two applications listen to the same broadcast event and set the priority, the first installed app receives the broadcast


Tip: If you need a broadcast recipient to receive a broadcast anyway, whether it's the lowest priority or a high-priority termination

Can call Sendorderedbroadcast (intent,null,receiver,null,0,null,null);

The first parameter is intent, and the third parameter is the specified broadcast receiver object

The Broadcastreceiver of Android development

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.