Android 52 Sticky Broadcast

Source: Internet
Author: User

Sticky broadcast: When the broadcast is sent out, the broadcast receiver has not yet been created and can be received when the broadcast receiver is registered, and if it is not a sticky broadcast, it will no longer be accepted without a broadcast receiver.

Mainactivity:

 Packagecom.sxt.day07_07;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Intent Intent=NewIntent ("Com.sxt.day07_07.my_receiver"); Sendstickybroadcast (intent);//send sticky broadcasts, stay waiting for the receiverSetlistener (); }    Private voidSetlistener () {Findviewbyid (r.id.btnstartsecondactivity). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent Intent2=NewIntent (mainactivity. This, Secondactivity.class); StartActivity (Intent2);//Start secondactivity            }        }); }}

Secondactivity

 Packagecom.sxt.day07_07;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.util.Log;ImportAndroid.view.Menu; Public classSecondactivityextendsActivity {myreceiver mreceiver;    Intent mintent; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_second);    Registerreceiver (); }    Private voidRegisterreceiver () {Mreceiver=NewMyreceiver (); Intentfilter Filter=NewIntentfilter ("Com.sxt.day07_07.my_receiver");    Registerreceiver (mreceiver, filter); }        //internal class, Myreceiver can register to receive broadcasts only if Secondactivity is started.     classMyreceiverextendsbroadcastreceiver{@Override Public voidOnReceive (Context context, Intent Intent) {log.i ("Main", "Myreceiver.onreceive ()"); Mintent=Intent; }} @Overrideprotected voidOnDestroy () {Super. OnDestroy (); Removestickybroadcast (mintent);//move out, move out, and you won't receive the broadcast.Unregisterreceiver (Mreceiver);//move out recipient    }}

System description File:

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "com.sxt.day07_07"Android:versioncode= "1"Android:versionname= "1.0" >    <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" />    <uses-permissionAndroid:name= "Android.permission.BROADCAST_STICKY"/>Sticky Broadcast to request permission<ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name= "Com.sxt.day07_07.MainActivity"Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>        <ActivityAndroid:name= "Com.sxt.day07_07.SecondActivity"Android:label= "@string/title_activity_second" >        </Activity>    </Application></Manifest>

Main Page:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <ButtonAndroid:id= "@+id/btnstartsecondactivity"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world" /></Relativelayout>

Second page:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Secondactivity " >    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world" /></Relativelayout>

Android 52 Sticky Broadcast

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.