Alexkn Android first line of code-7. Broadcast

Source: Internet
Author: User

The broadcast in 0.Android can be divided into two types, standard broadcast and ordered broadcast. Standard broadcast (normal broadcasts) is a fully asynchronous broadcast that, after broadcast, almost all broadcast receivers receive this broadcast message at the same time,

An ordered broadcast (Ordered broadcasts) is a synchronous broadcast that, after a broadcast, only one broadcast receiver can receive this broadcast message at the same time.

1. Monitoring System Broadcast (power/wifi/time zone change ... )

If you need to listen to a system broadcast, you need two steps:

(1) Rewrite the broadcastreceiver in the OnReceive method can be, to achieve their own receiver;

     Public class extends broadcastreceiver {        @Override        publicvoid  onreceive (context context, Intent Intent) {            Toast.maketext (context,"Network Change", Toast.length_short). Show ();        }    }

(2) Define an intent filter to filter out the broadcast

    protected void onCreate (Bundle savedinstancestate) {        super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);         New Intentfilter ();        Intentfilter.addaction ("Android.net.conn.CONNECTIVITY_CHANGE");         New networkchangereceiver ();        Registerreceiver (Networkchangereceiver,intentfilter);    }

(3) Register in Androidmainifest.xml

<? XML version= "1.0" encoding= "Utf-8" ?> <  xmlns:android= "http://schemas.android.com/apk/res/android"    package  = "Com.alex.learn.broadcasttest">    <android:name = "Android.permission.ACCESS_NETWORK_STATE" />

2. send a regular broadcast

(1) Implement a broadcast receiver Mybroadcastreceiver, define what to do when you receive a broadcast

 Public class extends broadcastreceiver {    @Override    publicvoid  onreceive (context context, Intent Intent) {        "received in Mybroadcastreceiver", Toast.length_short). Show ();    }}

(2) Implement a broadcast transmitter that defines the transmission of the broadcast

 Public classMainactivityextendsappcompatactivity {PrivateIntentfilter Intentfilter; PrivateNetworkchangereceiver Networkchangereceiver; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button Button=(Button) Findviewbyid (R.id.button); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent ("Com.example.broadcasttest.MY_BROADCAST");            Sendbroadcast (Intent);    }        }); }

Now that the Com.example.broadcasttest.MY_BROADCAST is bound to the intent, it is necessary to register a receiver in the Androidmainifest.xml

<manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.alex.learn.broadcasttest" > <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/ > <uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/> <Application Android:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme" >        ...        <receiver android:name= ". Mybroadcastreceiver "> <intent-filter> <action android:name=" com.example.broadcasttes T.my_broadcast "/> </intent-filter> </receiver></application>

Alexkn Android first line of code-7. Broadcast

Related Article

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.