Android BroadcastReceiver and broadcastreceiver

Source: Internet
Author: User

Android BroadcastReceiver and broadcastreceiver

Introduction:

BroadcastReceiver is essentially a global listener used to listen on global broadcast messages of the system. Therefore, it can easily implement communication between different components in the system.


Features:

1. BroadcastReceiver is used to receive the Broadcast Intent sent by the program, which is the same as the Activity and Service started by the application;

2. When an application sends a Broadcast Intent, all BroadcastReceiver matching the Intent may be started;


Usage:

The following figure shows the main use of broadcastreceiver:



Here we will use a small example to learn BroadcastReceiver:

Create three broadcast classes. The first two are registered in manifest, which is called static registration. The last one is not registered in manifest but registered in code, which is called Dynamic Registration, there are two differences between sending and implicit sending when sending a broadcast.

First, let's take a look at the definition of the three broadcast receiver classes and the content of the fragments in manifest are 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. broadcastreceiver. 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 broadcast, and the receiver will not receive messages in the future }}

        <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 five buttons for testing. 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 extends er; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); setupViews (); intentFilter = new IntentFilter (MyReceiver03.INTENT _ ACTION_MYRECEIVER03); receiver ER = new MyReceiver03 () ;}@ Overridepublic void onClick (View v) {switch. getId () {case R. id. button1: // explicitly send broadcast Intent intent = new Intent (MainActivity. this, MyReceiver01.class); intent. putExtra ("data", "bear"); sendBroadcast (intent); break; case R. id. button2: // send broadcast Intent intent02 = new Intent ("com. bear. broadcastreceiver. intent. action. receiver02 "); intent02.putExtra (" data "," bear02 "); sendBroadcast (intent02); break; case R. id. button3: // dynamically register the broadcast registerReceiver (receiver, intentFilter); break; case R. id. button4: Intent intent03 = new Intent (Intent _ ACTION_MYRECEIVER03); encrypt ("data", "bear03"); sendBroadcast (intent03); // sendOrderedBroadcast (intent03, receiverPermission ); send broadcast break by priority; case R. id. button5: // cancel the broadcast unregisterReceiver (receiver ER); 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 );}}


Summary:

1. each time a Broadcast event occurs, the system creates a corresponding BroadcastReceiver instance and automatically triggers its onReceive () method. After the onReceive () method is executed, broadcastReceiver instances will be destroyed;


2. If the BroadcastReceiver onReceive () method cannot be executed within 10 seconds, Android considers the program to be unresponsive. Therefore, do not perform time-consuming operations in the onReceive () method of the broadcast receiver; otherwise, the ANR (Application No Response) dialog box will pop up;


3. If you really need to perform a time-consuming operation based on the broadcast, you can use Intent to start a Service to complete this operation. You should not consider using a new thread to perform time-consuming operations. Because the life cycle of BroadcastReceiver itself is very short, it may be that the sub-thread may not end yet, And BroadcastReceiver has exited;


Finally, the entire demo project source code is attached here:

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