Android's Broadcastreceiver

Source: Internet
Author: User

Brief introduction:

Broadcastreceiver is essentially a global listener that listens to the system's global broadcast messages, so it can easily communicate between different components in the system.


Characteristics:

1.BroadcastReceiver is used to receive the broadcast Intent issued by the program, and the application starts the activity, the service is the same;

2. When the application emits a broadcast intent, all broadcastreceiver matching the intent may be started;


Usage:

A popular image on the internet can be very good to feel the main use of Broadcastreceiver:



This time we are going through a small example to learn Broadcastreceiver together:

Set up three broadcast classes, the first two in the manifest register, known as static registration, the last one is not registered in the manifest and registered in the code, called Dynamic registration, send broadcast when there are display send and implicit send two different.

First, let's take a look at the definition of the three broadcast receiver classes and the fragment content in manifest as follows:

Import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.widget.toast;public class MyReceiver01 extends broadcastreceiver{@Overridepublic void OnReceive (Context Context, Intent Intent) {String msg = Intent.getextras (). getString ("Data"); Toast.maketext (Context, "MyReceiver01 received message:" + MSG, Toast.length_short). Show ();}}

Import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.widget.toast;public class MyReceiver02 extends Broadcastreceiver {@Overridepublic void OnReceive (Context Context, Intent Intent) {String msg = Intent.getextras (). getString ("Data"); Toast.maketext (Context, "MYRECEIVER02 received message:" + MSG, Toast.length_short). Show ();}}

public class MyReceiver03 extends Broadcastreceiver {public final static String intent_action_myreceiver03 = "com.bear.br Oadcastreceiver.intent.action.receiver03 "; @Overridepublic void OnReceive (context context, intent intent) {String msg = Intent.getextras (). getString ("Data"); Toast.maketext (Context, "MYRECEIVER03 received message:" + MSG, Toast.length_short). Show ();//abortbroadcast (); Cancel the broadcast, subsequent receiver will not be able to receive the message}}

        <receiver android:name= "Com.bear.broadcastreceiver.MyReceiver01" ></receiver>                <receiver Android:name= "Com.bear.broadcastreceiver.MyReceiver02" >            <intent-filter >                <action android: Name= "Com.bear.broadcastreceiver.intent.action.receiver02"/>            </intent-filter>                    </receiver >


The main interface has a total of 5 buttons to test, and the main interface class is defined as follows:

Import Android.app.activity;import Android.content.intent;import Android.content.intentfilter;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;public class MainActivity Extends Activity implements Onclicklistener{private Intentfilter intentfilter;private MyReceiver03 receiver;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Setupviews (); intentfilter = new Intentfilter (MYRECEIVER03.INTENT_ACTION_MYRECEIVER03); Receiver = new MyReceiver03 ();} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.button1://explicitly send broadcast intent intent = new Intent (Mainactivi Ty.this, Myreceiver01.class) Intent.putextra ("Data", "Bear"); Sendbroadcast (intent); Break;case r.id.button2:// Implicitly send broadcast Intent intent02 = new Intent ("Com.bear.broadcastreceiver.intent.action.receiver02"); Intent02.putextra ("Data") , "Bear02"); Sendbroadcast (intent02); break;case r.id.button3://Dynamic Register broadcast Registerreceiver (Receiver, Intentfilter); break;case r.id.button4:intent intent03 = new Intent (myreceiver03.intent_action_myreceiver03 ); Intent03.putextra ("Data", "bear03"); Sendbroadcast (intent03);//sendorderedbroadcast (intent03, Receiverpermission); Send broadcast Break;case by priority r.id.button5://Logoff broadcast unregisterreceiver (receiver); break;default:break;}} private void Setupviews () {Findviewbyid (r.id.button1). Setonclicklistener (this); Findviewbyid (R.id.button2). Setonclicklistener (This), Findviewbyid (R.id.button3) Setonclicklistener (this); Findviewbyid (R.id.button4). Setonclicklistener (This), Findviewbyid (R.ID.BUTTON5) Setonclicklistener (this);}}


Summarize:

1. After each system broadcast (broadcast) event occurs, the system creates the corresponding Broadcastreceiver instance and automatically triggers its onreceive () method, after the OnReceive () method finishes executing, Instances of Broadcastreceiver will be destroyed;


2. If the Broadcastreceiver onreceive () method cannot be completed within 10 seconds, Android will consider the program unresponsive. So do not perform some time-consuming operations in the OnReceive () method of the broadcast receiver, or the Application No Response dialog box will pop up ;


3. If you do need to perform a more time-consuming operation based on the broadcast, consider starting a service with intent to complete the operation. You should not consider using a new thread to complete the time-consuming operation, because the broadcastreceiver itself has a very short life cycle, and it is possible that the child thread may not have ended and Broadcastreceiver has exited;


Finally, I enclose the entire demo project source code here:

Android Broadcastreceiver

Android's 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.