------Static registration of two kinds of registration methods of Broadcastreceiver

Source: Internet
Author: User

Activity_main.xml

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent">    <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:textsize= "24dip"android:gravity= "Center"Android:text= "Broadcastreceiver Demo"/>            <ButtonAndroid:id= "@+id/send_static"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Send custom static registered broadcast" /></LinearLayout>

Androidmanifest.xml

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.my.broaccast"Android:versioncode= "1"Android:versionname= "1.0" >    <USES-SDKandroid:minsdkversion= "+"android:targetsdkversion= "+" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name=". 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 custom static broadcast receiver -        <receiverAndroid:name=". Staticreceiver ">            <Intent-filter>                <ActionAndroid:name= "Com.my.staticreceiver" />            </Intent-filter>        </receiver>    </Application></Manifest>

Mainactivity.java

 PackageCom.my.broaccast;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {PrivateButton Sendstaticbutton; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Sendstaticbutton=(Button) Findviewbyid (r.id.send_static); Sendstaticbutton.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubIntent Intent=NewIntent (); Intent.setaction ("Com.my.staticreceiver"); Intent.putextra ("MSG", "Receive static registration broadcast successfully!" ");            Sendbroadcast (Intent);    }        }); }}

Staticreceiver.java

 PackageCom.my.broaccast;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.widget.Toast; Public classStaticreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {//TODO auto-generated Method Stub        if("Com.my.staticreceiver". Equalsignorecase (Intent.getaction ())) {String msg= Intent.getstringextra ("msg"));        Toast.maketext (Context, MSG, toast.length_short). Show (); }    }}

------Static registration of two kinds of registration methods of Broadcastreceiver

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.