Android Development Broadcast Learning notes

Source: Internet
Author: User

There are too many broadcasts in Android, just a little summary today.
There are two ways to register by:

1. Register the broadcast statically:
Static registration broadcast is to register the broadcast in the Androidmanifest.xml file, suppose we want to achieve such an effect, click on a button on an activity, send a broadcast, this broadcast pop up a toast, display "static" word.

First look at the broadcast recipient:

publicclass MyBroadcast extends BroadcastReceiver {    @Override    publicvoidonReceive(Context context, Intent intent) {        Toast.makeText(context,"静态", Toast.LENGTH_LONG).show();    }}

Register in the manifest file:

        <receiver android:name="com.example.staticbroadcast.MyBroadcast" >            <intent-filter>                <action android:name="com.test.StaticBroadcast" />            </intent-filter>        </receiver>

Click events in activity (send broadcast):

        this.static_btn.setOnClickListener(new OnClickListener() {            @Override            publicvoidonClick(View v) {                new Intent();                intent.setAction("com.test.StaticBroadcast");                sendBroadcast(intent);            }        });

2. Dynamic registration.
Dynamic registration is typically registered in the activity's OnStart () method, and is de-registered in the OnStop () method, with the following code:

 Public  class mainactivity extends Activity {    PrivateButton static_btn;PrivateButton dynamic_btn;PrivateBroadcastreceiver Mybroadcastreceiver;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); This. static_btn = (Button) This. Findviewbyid (R.id.button1); This. dynamic_btn = (Button) This. Findviewbyid (R.id.button01); Mybroadcastreceiver =NewBroadcastreceiver () {@Override             Public void OnReceive(context context, Intent Intent) {Toast.maketext (mainactivity). This,"Hello, here is the dynamic broadcast!" ", Toast.length_long). Show (); }        };//This.static_btn.setOnClickListener (new Onclicklistener () {////@Override//public void OnClick (View v) {//Intent Intent = new Intent ();//Intent.setaction ("Com.test.StaticBroadcast");//Sendbroadcast (intent);//          }//      });         This. Dynamic_btn.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View v) {//Send broadcastIntent Intent =NewIntent (); Intent.setaction ("Dynamicbroadcast");            Sendbroadcast (Intent);    }        }); }@Override    protected void OnStart() {Super. OnStart ();//Registered broadcastIntentfilter Intentfilter =NewIntentfilter (); Intentfilter.addaction ("Dynamicbroadcast");    Registerreceiver (Mybroadcastreceiver, Intentfilter); }@Override    protected void OnStop() {Super. OnStop ();//Cancel RegistrationUnregisterreceiver (Mybroadcastreceiver); }}

Details about the static registration:
The android:exported= "true" property indicates whether the broadcast receiver receives broadcasts from other apps and, if there is a Intent-filter attribute, the default is true, otherwise the default is False.

Each broadcast recipient can accept multiple broadcast sources, and if they are statically registered, you do this:

        <receiverandroid:exported="true"android:name=" Com.example.staticbroadcast.MyBroadcast " >                                     <intent-filter>                <action android:name="Com.test.StaticBroadcast" />                <action android:name="Com.test.StaticBroadcast2"/>            </intent-filter>        </receiver>

This is handled in the broadcast receiver:

 @Override public void OnReceive (context context, Intent Intent) {if (Intent.getaction  () .equals  (  "Com.test.StaticBroadcast" )) {Toast.maketext  (context,  "static" , Toast _short) .show  ()  } else if (Intent.getaction  () .equals  ( "Com.test.StaticBroadcast2" )) {toast (context,  "Static 2" , Toast _short) .show  ()  } }

If you are registering dynamically, the registration method is as follows:

    @Override    protectedvoidonStart() {        super.onStart();        //注册广播        new IntentFilter();        intentFilter.addAction("DynamicBroadcast");        intentFilter.addAction("DynamicBroadcast2");        registerReceiver(myBroadcastReceiver, intentFilter);    }

The broadcast receivers are handled in the same way as static registrations.

about how to use broadcast to communicate between activity and fragment can see my other blog using broadcast to implement communication between Android components

This article refers to the following: http://www.cnblogs.com/lwbqqyumidi/p/4168017.html

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.

Android Development Broadcast Learning notes

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.