Android Broadcast Basics

Source: Internet
Author: User

Radio plays a role in Android as a school horn.

Broadcasting is a kind of cross-process communication, which is divided into standard broadcast and orderly broadcast. Standard broadcast is a completely asynchronous broadcast, which is more efficient and means it will not be intercepted, and an ordered broadcast is a fully synchronous operation, and the broadcast receiver is orderly when it receives that type of broadcast, and has the right to intercept the broadcast.

Dynamic registration:

1> creates a broadcast receive class, inherits Broadcastreceiver, and overrides the parent class OnReceive method;

2> Monitor Filter:

Intentfilter intentfilter= new Intentfilter ();

Intentfilter.setaction ("Android.net.conn.CONNECTIVITY_CHANGE");

Networkrecvice = new Networkrecvice ();

Registerreceiver (Networkrecvice,intentfilter);

3> cancelling a broadcast

Unregistereceiver (Networkrecvice);

Static registration:

To register in Androidmanifest.xml:

<receiver

Android:name= ". Networkrecvice"

<intent-filter>

<action android:name= "Android.net.conn.CONNECTIVITY_CHANGE" >

<intent-filter>

</receiver>

Send an ordered broadcast:

Send broadcast:

Intent Intent = new Intent ("Android.net.conn.CONNECTIVITY_CHANGE");

Sendorderedbroadcast (Intent,null);

Set the priority level:

<receiver

Android:name= ". Networkrecvice"

<intent-filter android:priority= ">"

<action android:name= "Android.net.conn.CONNECTIVITY_CHANGE" >

<intent-filter>

</receiver>

Intercept broadcasts:

Abortbroadcast ();

Use local broadcast:

All broadcasts sent and received above belong to the system global broadcast, that is, the broadcast can be received by any other application, and can also accept broadcasts from any other program. To solve the security Jon Question, Android introduced the "Local broadcast system".

Registering and receiving local broadcasts differs little from system broadcasts, mainly because of the increased use of Localbroadcastmanager to manage broadcasts.

Send:

Localbroadcastmanager Manager = Localbroadcastmanager.getinstance (this);

.........

Manager.sendbroadcast (Intent);

The use of radio is relatively simple, the main master of its two different registration methods, according to the needs of specific applications.

Android Broadcast Basics

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.