The broadcast of Android learning

Source: Internet
Author: User

1. Broadcasting mechanisms

Standard broadcast:

Standard broadcast (normal broadcasts) is a fully asynchronous broadcast that, after a broadcast, all broadcast receivers receive this broadcast message almost at the same time, so there is no sequence between them, and it cannot be truncated. Work Flow chart:

Orderly broadcast:

Ordered broadcast (Ordered broadcasts) is a synchronous broadcast, after the broadcast, only one broadcast receiver will be able to receive this broadcast message at the same time, and when the logic in this broadcast receiver is completed, the broadcast will continue to pass. So at this time the broadcast receiver is in order, the priority of the broadcast receiver can receive a broadcast message, and the front of the broadcast receiver can also truncate the broadcast is being delivered, so that the subsequent broadcast receivers will not be able to receive broadcast messages. Work Flow chart:

2. Receiving system broadcasts:

There are generally two ways of registering a broadcast, registering it in code and registering it in Androidmanifest.xml, where the former is also known as dynamic registration, which is also known as static registration.

Dynamic registration:

To create a new project, modify the code in Mainactivity:

 Public classMainactivityextendsActivity {PrivateIntentfilter Intentfilter; PrivateNetworkchangereceiver Networkchangereceiver; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); Intentfilter=NewIntentfilter (); Intentfilter.addaction ("Android.net.conn.CONNECTIVITY_CHANGE"); Networkchangereceiver=NewNetworkchangereceiver ();  Registerreceiver (Networkchangereceiver, Intentfilter); } @Overrideprotected voidOnDestroy () {Super. OnDestroy ();  Unregisterreceiver (Networkchangereceiver); }classNetworkchangereceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {toast.maketext (context,"Network Changes", Toast.length_short). Show (); }}}

We define an inner class networkchangereceiver in Mainactivity, which inherits from Broadcastreceiver. The OnReceive () method of the parent class is overridden, and the OnReceive () method is executed whenever the network transformation occurs.

Then observing the OnCreate () method, we first created an instance of Intentfilter and added a action with a value of Android.net.conn.CONNECTIVITY_CHANGE, and when the network state changed, the system sent Out is a broadcast with a value of Android.net.conn.CONNECTIVITY_CHANGE, which means that our broadcast receivers want to listen to what broadcasts, just add the corresponding action here. Then call Registerreceiver ()

method to register. So Networkchangereceiver will receive all the values of Android.net.conn.CONNECTIVITY_CHANGE broadcast, but also to achieve the ability to listen to network changes.

Dynamically registered broadcast receivers must be unregistered only, here we are in the OnDestroy () method by calling the Unregisterreceiver () method to achieve.

Static registration:

Dynamically registered broadcast receivers have the freedom to control registration and logoff, there is a great advantage in flexibility, but it also has a disadvantage, that is, the program must be launched to receive the broadcast, static registration can avoid this kind of situation.

Create a new class bootcompletereceiver inherit from Broadcastreceiver

 Public class extends broadcastreceiver {@Override  publicvoid' Boot complete ', Toast.length_long)  . Show (); }}

Modify the Androidmanifest.xml file

<Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.broadcasttest"Android:versioncode= "1"Android:versionname= "1.0" >...<uses-permissionAndroid:name= "Android.permission.ACCESS_NETWORK_STATE" />    <uses-permissionAndroid:name= "Android.permission.RECEIVE_BOOT_COMPLETED" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >...<receiverAndroid:name=". Bootcompletereceiver " >        <Intent-filter>          <ActionAndroid:name= "Android.intent.action.BOOT_COMPLETED" />        </Intent-filter>      </receiver>    </Application></Manifest>

A new label <receiver> appears in the <application> tab, and all statically registered broadcast receivers are registered here. Its usage is very similar to the <activity> tag, first through the Android:name to specify which broadcast receiver to register, and then in the <intent-filter> tag to add the broadcast you want to receive, because When the Android system starts, a broadcast with a value of Android.intent.action.BOOT_COMPLETED is issued, so we've added the appropriate action here.

In addition, the monitoring system to start the broadcast is also required to declare permissions, you can see, we use the <uses-permission> tag and added a Android.permission.RECEIVE_BOOT_COMPLETED permission.

This will be able to do the boot to receive the broadcast.

Not finished ~

The broadcast of Android learning

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.