Four major Android components-Use of BroadcastReceiver and broadcastreceiver
BroadcastReceiver is also known as the broadcast receiver. Since it is used to receive broadcasts, someone must be responsible for sending them.
Broadcast in Android:
In real life, we all know what broadcast is and what it is used. For example, the broadcasting in the park mainly informs tourists of what happened and what to do,
What should not be done. Broadcasting in Android is basically the same as broadcasting in real life. It is mainly used for message transmission.
The broadcast receiver can be dynamically registered in java code or in the AndroidManifest file.
Ordered broadcast and disordered Broadcast
Let's talk about unordered broadcast first. We still use the broadcast in the park as an example. As long as the broadcast is sent out, everyone basically hears it at the same time.
It is asynchronously transmitted, Which is unordered broadcast. It features high transmission speed. However, ordered broadcast cannot be intercepted or terminated.
Unordered broadcast is sent using the sendBroadcast method.
Ordered broadcast is sent out in a certain order. A receiver with a high permission receives the broadcast information first. The receiver can modify the information in the broadcast,
Then spread the broadcast to the lower-level, or terminate the broadcast.
Create BroadcastReceiver class
By simply inheriting the BroadcastReceive class and implementing the onReceive method, you can create your own broadcast receiver class.
private class NormalBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { String data = intent.getStringExtra("data"); Toast.makeText(getBaseContext(), data, Toast.LENGTH_SHORT).show(); } }
Complete code:
In the following code snippet, we use the onResume method to register the broadcast receiver and cancel the registration of the broadcast receiver in the onPause method.
Of course, you can also register in the AndroidManifest file. Once registered, the broadcast receiver cannot be canceled.
Package com. whathecode. broadcastreceiverdemo; import android. app. activity; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. content. intentFilter; import android. OS. bundle; import android. view. view; import android. widget. toast; public class MainActivity extends Activity {// instantiate broadcast receiver NormalBroadcastReceiver nbr = new NormalBroadcastReceiver (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main) ;}@ Override protected void onResume () {super. onResume (); // register the broadcast receiver registerReceiver (nbr, new IntentFilter ("com. whathecode. broadcastreceiverdemo ");} @ Override protected void onPause () {super. onPause (); // cancel registering the broadcast receiver unregisterReceiver (nbr);} public void sendOrder (View view) {Intent orderBroadcast = new Intent (); orderBroadcast. setAction ("com. whathecode. broadcastreceiverdemo "); orderBroadcast. putExtra ("data", "I am an ordered broadcast"); // send an ordered broadcast sendOrderedBroadcast (orderBroadcast, null);} public void sendDisorder (View view) {Intent intent = new Intent (); intent. setAction ("com. whathecode. broadcastreceiverdemo "); intent. putExtra ("data", "I am unordered broadcast"); // send unordered broadcast sendBroadcast (intent);} // inherits the BroadcastReceiver class, implementation of onReceive method private class NormalBroadcastReceiver extends BroadcastReceiver {/*** this method is called when the broadcast is received */@ Override public void onReceive (Context context, Intent intent) {// obtain information in the broadcast and print it out with Toast String data = intent. getStringExtra ("data"); Toast. makeText (getBaseContext (), data, Toast. LENGTH_SHORT ). show ();}}}
Running effect: