Android 48 broadcast

Source: Internet
Author: User

The system starts to reboot will send the power-on restart broadcast, low power will send low-battery broadcast, broadcast registration has 2 kinds: System description file XML registration and Java code Registration, the former is static registration (global registration) The latter is a dynamic registration (relying on the time of the building, component destruction will not receive the broadcast).

Activity.java

 Packagecom.sxt.day07_04;Importandroid.app.Activity;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener; Public classMainactivityextendsActivity {MyReceiver2 mreceiver; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Registerreceiver ();    Setlistener (); }    Private voidSetlistener () {Sendbroadcast1clicklistener ();    Sendbroadcast2clicklistener (); }    Private voidSendbroadcast2clicklistener () {Findviewbyid (r.id.btnsendbroadcast_2). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent ("Com.sxt.day07_04.MyReceiver2");//Intent.putextra ("Key", "Hello android!");            Sendbroadcast (Intent);    }        }); }    Private voidSendbroadcast1clicklistener () {Findviewbyid (r.id.btnsendbroadcast_1). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent ("Com.sxt.day07_04.MyReceiver1"); Intent.putextra ("Key", "Hello java!");            Sendbroadcast (Intent);    }        }); }    //broadcast receivers for Java class registrations    Private voidRegisterreceiver () {Mreceiver=NewMyReceiver2 (); Intentfilter Filter=NewIntentfilter ();//Intent FilterFilter.addaction ("Com.sxt.day07_04.MyReceiver2");//the broadcast stringRegisterreceiver (mreceiver, filter); }    classMyReceiver2extendsbroadcastreceiver{@Override//method of receiving broadcast execution         Public voidOnReceive (Context context, Intent Intent) {log.i ("Main", "Myreceiver2.onreceive ():" +intent.getstringextra ("key")); }} @Overrideprotected voidOnDestroy () {Super. OnDestroy (); //when the activity exits, cancel the broadcast registration and release the resources.Unregisterreceiver (Mreceiver); }}

Broadcast 1.java

 Packagecom.sxt.day07_04;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.util.Log; Public classMyReceiver1extendsBroadcastreceiver {@Override//methods to be executed after receiving a broadcast     Public voidOnReceive (Context context, Intent Intent) {log.i ("Main", "Myreceiver1.onreceive ():" +intent.getstringextra ("key")); }}

System description File:

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.sxt.day07_04"Android:versioncode= "1"Android:versionname= "1.0" >    <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name= "Com.sxt.day07_04.MainActivity"Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>registering a global broadcast recipient<receiverAndroid:name= "Com.sxt.day07_04.MyReceiver1">broadcast receiver Class<Intent-filter>                <ActionAndroid:name= "Com.sxt.day07_04.MyReceiver1"/>The broadcast string is the one that can receive the broadcast</Intent-filter>        </receiver>    </Application></Manifest>

Android 48 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.