Android Basic---->broadcast use

Source: Internet
Author: User

In Android, broadcast is a widely used mechanism for transferring information between applications. Broadcastreceiver is a class of components that are filtered to receive and respond to the sent broadcast.

Directory Navigation:

    1. brief description of broadcast broadcast
    2. Preparations for the broadcast broadcast
    3. Test of broadcast broadcast
    4. Friendship Link

Brief description of Broadcast broadcast

I. Types of broadcasts: standard Broadcasting and orderly broadcasting

    • Standard broadcast: is a completely asynchronous broadcast, after the broadcast, all broadcast receivers almost all at the same time to receive the broadcast message, so there is no order between them. This kind of broadcast is more efficient, but it also means it can't be truncated.

    • Orderly broadcast: It 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

  

Two ways to register Broadcastreceiver:

    • Static registration: Register with Tag life in Androidmanifest.xml, and set filters in tags with tags
<receiver android:name= ". Networkchangereceiver ">    <intent-filter>        <action android:name=" Com.example.linux.broadcasttest.MyBroadcast "/>    </intent-filter></receiver>
    • Dynamic registration: Calling the Registerreceiver (broadcastreceiver receiver, Intentfilter Filter) method in Java code
New Intent (); Intent.putextra ("username", "Linux"); Intent.setaction (" Com.example.linux.broadcasttest.MyBroadcast "); Sendbroadcast (intent) ;

Preparations for the broadcast broadcast

We build an Android project with the following structure:

First, some information is initialized in mainactivity:

Private Final StaticString TAG = "Otherreceiver";PrivateIntentfilter Intentfilter;Privatenetworkchangereceiver Networkchangereceiver;PrivateLocalbroadcastmanager Localbroadcastmanager;Privatelocalreceiver localreceiver; @Overrideprotected voidonCreate (Bundle savedinstancestate) {log.i (TAG,"Main on Create"); Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main);}

II. Create three broadcast receivers for broadcast testing: they are as follows

    • Localreceiver Local Broadcast:
 Packagecom.example.linux.broadcasttest;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.util.Log;/*** Created by Linux on 2016/3/7.*/ Public classLocalreceiverextendsBroadcastreceiver {Private Final StaticString TAG = "Otherreceiver";  PublicLocalreceiver () {log.i (TAG,"Local receiver constructor."); } @Override Public voidOnReceive (Context context, Intent Intent) {String username= Intent.getstringextra ("username"); LOG.I (TAG,"Local username:" +username); }}
    • Otherreceiver is used to test ordered broadcasts:
 Packagecom.example.linux.broadcasttest;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.util.Log; Public classOtherreceiverextendsBroadcastreceiver {Private Final StaticString TAG = "Otherreceiver";  PublicOtherreceiver () {log.i (TAG,"Other receiver constructor."); } @Override Public voidOnReceive (Context context, Intent Intent) {String username= Intent.getstringextra ("username"); LOG.I (TAG,"Other Username:" +username);    Abortbroadcast (); }}
    • Networkchangereceiver used to test system broadcasts:
 Packagecom.example.linux.broadcasttest;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.util.Log;/*** Created by Linux on 2016/3/7.*/ Public classNetworkchangereceiverextendsBroadcastreceiver {Private Final StaticString TAG = "Otherreceiver";  PublicNetworkchangereceiver () {log.i (TAG,"Network receiver constructor."); } @Override//Intent is a parameter in the Sendbroadcast     Public voidOnReceive (Context context, Intent Intent) {String username= Intent.getstringextra ("username"); LOG.I (TAG,"Network username:" +username); }}

Third, because the broadcast will involve registration, here I put the registration code of the broadcast: in the Androidmanifest.xml file

<receiverAndroid:name=". Networkchangereceiver ">    <Intent-filter>        <ActionAndroid:name= "Com.example.linux.broadcasttest.MyBroadcast" />    </Intent-filter></receiver><receiverAndroid:name=". Otherreceiver ">    <Intent-filterandroid:priority= "+">        <ActionAndroid:name= "Com.example.linux.broadcasttest.MyBroadcast" />    </Intent-filter></receiver><receiverAndroid:name=". Localreceiver ">    <Intent-filter>        <ActionAndroid:name= "Com.example.linux.broadcasttest.MyBroadcast" />    </Intent-filter></receiver>

Test of broadcast broadcast

I. Define method SENDSYSTEMBROAD for sending system broadcasts

// Send system broadcast  Public void sendsystembroad (view view) {    new  intentfilter ();    Intentfilter.addaction ("Android.net.conn.CONNECTIVITY_CHANGE");     New networkchangereceiver ();    Registerreceiver (Networkchangereceiver, intentfilter);}
    • Printed results:
03-23 23:03:20.087 31259-31259/com.example.linux.broadcasttest i/otherreceiver:other receiver constructor. 03-23 23:03:20.087 31259-31259/com.example.linux.broadcasttest i/otherreceiver:other username:linux03-23 23:03:20.097 31259-31259/com.example.linux.broadcasttest i/otherreceiver:network receiver constructor. 03-23 23:03:20.107 31259-31259/com.example.linux.broadcasttest i/otherreceiver:network username:linux 03-23 23:03:20.107 31259-31259/com.example.linux.broadcasttest i/otherreceiver:local receiver constructor. 03-23 23:03:20.107 31259-31259/com.example.linux.broadcasttest i/otherreceiver:local username:linux

Second, the definition method Sendbroadcast, uses in the test sends the standard broadcast

// send a standard broadcast  Public void sendbroadcast (view view) {    new  Intent ();    Intent.putextra ("username", "Linux");    Intent.setaction ("Com.example.linux.broadcasttest.MyBroadcast");    Sendbroadcast (intent);}
    • Printed results:
03-23 23:05:47.957 31259-31259/com.example.linux.broadcasttest i/otherreceiver:network receiver constructor. NULL

Iii. Defining method Sendorderbroad for testing ordered broadcasts

// send an ordered broadcast  Public void sendorderbroad (view view) {    new  Intent ();    Intent.putextra ("username", "Linux");    Intent.setaction ("Com.example.linux.broadcasttest.MyBroadcast");     NULL );}
    • Printed results:
03-23 23:07:28.877 31259-31259/com.example.linux.broadcasttest i/otherreceiver:other receiver constructor. 03-23 23:07:28.877 31259-31259/com.example.linux.broadcasttest I/otherreceiver:other username:linux

Iv. Defining method Sendlocalbroad for testing local broadcasts

// use local broadcast  Public void sendlocalbroad (view view) {    = localbroadcastmanager.getinstance (this);     New Intent ();    Intent.putextra ("username", "Linux");    Intent.setaction ("Com.example.linux.broadcasttest.MyBroadcast");    Localbroadcastmanager.sendbroadcast (intent); }
    • Printed results:
03-23 23:28:09.327 31259-31259/com.example.linux.broadcasttest i/otherreceiver:local username:linux03-23 23:28:09.327 31259-31259/com.example.linux.broadcasttest i/otherreceiver:local username:linux03-23 23:28:09.327 31259-31259/com.example.linux.broadcasttest i/otherreceiver:local Username:linux

Friendship Link
    • Broadcast test source code access password 1e16

Android Basic---->broadcast use

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.