Broadcast broadcast Receiver

Source: Internet
Author: User

Type of broadcast:

1. Standard broadcast (normal broadcat)

Broadcasts that are executed completely asynchronously, all broadcast receivers receive broadcast messages at the same time.

High efficiency, but cannot be truncated.

2. Orderly Broadcasting

Simultaneous broadcast, only one broadcast receiver can receive broadcast messages at the same time, and will not continue to be delivered until the execution is complete.

High priority receives the broadcast first, and can truncate the broadcast.

Receive system broadcast:

Registration: Dynamic registration (code) and static registration (Androidamanifest.xml)

Dynamic Broadcast:

1. Inheriting Broadcastreceiver

2. Rewrite the OnReceive () method

3, Registered Registerreceiver ()

4, the dynamic registration of the broadcast receiver must be unregistered, unregisterreceiver ()

5, the Declaration of Rights in the Androidmanifest.xml

Static broadcast receivers are registered through the <receiver> tag in the Androidmanifest.xml,

<receiver android:name= ". Bootcompletereceiver ">
<intetn-filter>
<action android:name= "Android.intent.action.BOOT_COMPLETED" >//Start-up broadcast
</intent-filter>
</recevier>

Open threads are not allowed in the broadcast receiver, do not add too much logic, or take any time-consuming actions.

Custom broadcasts:

1. Standard Broadcast:

(1) Define a broadcast receiver

(2) Adding custom broadcast logic to the event

(3) Building the intent object, passing in the value of the broadcast to be sent

(4) Call the Sendbroadcast () method to send the broadcast

(5) <receiver> registration in Androidmanifest.xml

2. Orderly Broadcasting:

Basically, as with standard broadcasts, the Send call Sendorderedbroadcast () method.

Set priority in Androidmanifest.xml <intent-filter android:priority= ">"

Truncate broadcast call Abortbroadcast () method

Local broadcast: (addressing security issues)

Use Localbroadcastmanager to manage broadcasts.

(1) Get an example of him through Localbroadcastmanager.getinstance (this)

(2) Building Localreceiver class

(3) Dynamic registration via Localbroadcastmanager's Registerreceiver () method. The method contains 2 parameters Localreceiver and Intentfilter

(4) Call Localbroadcastmanager's Sendbroadcast () method to send the broadcast

(5) Cancellation of registration in the OnDestroy () method

Local broadcasts cannot be received by using static registration.

Broadcast broadcast Receiver

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.