Analysis of BroadcastReceiver usage examples in Android Development
This example describes how to use BroadcastReceiver in Android development. Share it with you for your reference. The specific analysis is as follows:
In Android, Broadcast is a mechanism for transmitting data (Intent) between components.
Braodcast Receiver is a broadcast Receiver. It is similar to the event processing mechanism, but the event processing mechanism is program component-level (for example, click an event of a button ), the broadcast event processing mechanism is system-level. We can use Intent to start a component, or use the sendBroadcast () method to initiate a system-level event broadcast to transmit messages. We can also implement the Broadcast Receiver in our own applications to listen to and respond to the Broadcast Intent.
Event broadcasts are sent by creating an Intent object and calling the sendBroadcast () method. Event acceptance is implemented by defining a class that inherits BroadcastReceiver, inheriting the class and overwriting its onReceive () method to respond to the event.
Many standard Broadcast actions are defined in the android system to respond to Broadcast events in the system.
① ACTION_TIME_CHANGED (triggered when the time changes)
② ACTION_BOOT_COMPLETED (triggered after the system is started)-for example, some programs are started after startup in this way.
③ ACTION_PACKAGE_ADDED (triggered when a package is added)
④ ACTION_BATTERY_CHANGED (triggered when the power is low)
The following is an example:
We bind an event to a button, and the event triggers logcat to output a log by sending a broadcast.
First look at the manifest file.
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" Package = "com. example. test" Android: versionCode = "1" Android: versionName = "1.0" type = "codeph" text = "/codeph"> <Uses-sdk Android: minSdkVersion = "8" Android: targetSdkVersion = "16"/> <Application Android: allowBackup = "true" Android: icon = "@ drawable/ic_launcher" Android: label = "@ string/app_name" Android: theme = "@ style/AppTheme"> <Activity Android: name = "com. example. broadcast. BroadcastReceiverActivity" Android: label = "@ string/app_name_bc"> <Intent-filter> <Action android: name = "android. intent. action. MAIN"> </Action> <Category android: name = "android. intent. category. LAUNCHER"> </Category> </Intent-filter> </Activity> <Cycler android: name = "com. example. broadcast. HelloBroadReciever"> <Intent-filter> <Action android: name = "comz. test. printlog"> </Action> </Intent-filter> </Cycler> </Application> </Manifest> |
The above declares a receiver. Receives a message named comz. test. printlog.
View activity:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
Package com. example. broadcast; Import android. app. Activity; Import android. content. Intent; Import android. OS. Bundle; Import android. util. Log; Import android. view. View; Import android. widget. Button; Import com. example. test. R; Public class BroadcastReceiverActivity extends Activity { @ Override Public void onCreate (Bundle savedInstanceState ){ Super. onCreate (savedInstanceState ); SetContentView (R. layout. activity_main ); Button b1 = (Button) findViewById (R. id. broadcastBtn ); B1.setOnClickListener (new View. OnClickListener (){ @ Override Public void onClick (View v ){ Log. e ("mason", "here "); // Define an intent Intent intent = new Intent (). setAction ("comz. test. printlog ") . PutExtra ("info", "here is your info ."); // Broadcast SendBroadcast (intent ); } }); } } |
In this Code, define an intent and send the broadcast.
Let's look at the broadcaster code:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Package com. example. broadcast; Import android. content. BroadcastReceiver; Import android. content. Context; Import android. content. Intent; Import android. util. Log; Public class HelloBroadReciever extends BroadcastReceiver { // If the received event occurs @ Override Public void onReceive (Context context, Intent intent ){ Log. e ("mason", "on receive "); If (intent. getAction (). equals ("comz. test. printlog ")){ Log. e ("mason", intent. getStringExtra ("info ")); } } } |
This is the BroadcastReceiver code.
After receiving a message, if the message is comz. test. printlog, the message is printed.
I hope this article will help you design your Android program.