broadcast Receiver usage
  1, broadcast receiver introduction
2, broadcast Receiver reception system's own broadcast
3, custom broadcast
I. Introduction to broadcast receiver
The four components in android are Activity, Service, broadcast, and content Provider. Intent is an abstract description of actions and actions that are responsible for message passing between components. Then the broadcast receiver component provides a mechanism to broadcast intent as a message and to react to it by all programs that are interested in it.
Second, broadcast receiver receiver system comes with the broadcast
We do an example of a feature that plays a piece of music when the system starts.
1, create a project lesson21_broadcastreceiver, copy a piece of music into the Res/raw directory
2, create Hellobroadcastreceiver.java content as follows:
java code:
- Package Android.demo;
- Import Android.content.BroadcastReceiver;
- Import Android.content.Context;
- Import android.content.Intent;
- Import Android.media.MediaPlayer;
- Import Android.util.Log;
- public class Hellobroadreciever extends Broadcastreceiver {
- If the received event occurs
- @Override
- public void OnReceive (context context, Intent Intent) {
- The output log
- LOG.E ("Hellobroadreciever", "boot_completed!!!!!!!!!!!!!!!!!!!!!!!!!");
- LOG.E ("Hellobroadreciever", "" +intent.getaction ());
- Then play a piece of music
- Mediaplayer.create (context, R.raw.babayetu). Start ();
- }
- }
Copy Code
3. Register this receiver in Androidmanifest.xml:
Java code:
- <?xml version= "1.0" encoding= "Utf-8"?>
- <manifest xmlns:android= "http://schemas.android.com/apk/res/android" Android:versionname= "1.0" Android: Versioncode= "1" package= "Android.basic.lesson21" >
- <application android:icon= "@drawable/icon" android:label= "@string/app_name" >
- <activity android:label= "@string/app_name" Android:name= ". Mainbroadcastreceiver ">
- <intent-filter= "" >
- <action android:name= "Android.intent.action.MAIN" >
- <category android:name= "Android.intent.category.LAUNCHER" >
- </category></action></intent>
- </activity>
- <!--define broadcast Receiver to specify the action of the listener--
- <receiver android:name= "Hellobroadreciever" >
- <intent-filter= "" >
- <action android:name= "Android.intent.action.BOOT_COMPLETED" >
- </action></intent>
- </receiver>
- </application></manifest>
Copy Code
III. Custom Broadcasts
Below we learn to make a broadcast. Let's go on with the example we just made.
4. Fill in the following code in Mainbroadcastreceiver.java:
Java code:
- Package Android.demo;
- Import android.app.Activity;
- Import android.content.Intent;
- Import Android.os.Bundle;
- Import Android.view.View;
- Import Android.widget.Button;
- public class Mainbroadcastreceiver extends Activity {
- /** called when the activity is first created. */
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Button B1 = (button) Findviewbyid (R.ID.BUTTON01);
- B1.setonclicklistener (New View.onclicklistener () {
- @Override
- public void OnClick (View v) {
- Define a intent
- Intent Intent = new Intent (). Setaction (
- "Android.basic.lesson21.Hello"). PutExtra ("Yaoyao",
- "Yaoyao is 189 days old, weeks-2010-08-10");
- Broadcast out
- Sendbroadcast (Intent);
- }
- });
- }
- }
Copy Code
5. Change Hellobroadreceiver.java content as follows:
Java code:
- Package Android.demo;
- Import Android.content.BroadcastReceiver;
- Import Android.content.Context;
- Import android.content.Intent;
- Import Android.media.MediaPlayer;
- Import Android.util.Log;
- public class Hellobroadreciever extends Broadcastreceiver {
- If the received event occurs
- @Override
- public void OnReceive (context context, Intent Intent) {
- Compare action to decide what information to output
- if (Intent.getaction (). Equals ("Android.intent.action.BOOT_COMPLETED")) {
- LOG.E ("Hellobroadreciever", "boot_completed!!!!!!!!!!!!!!!!!!!!!!!!!");
- }
- if (Intent.getaction (). Equals ("Android.basic.lesson21.Hello")) {
- LOG.E ("Hellobroadreciever", "Say Hello to Yaoyao!!!!!!!!!!!!!!!!!!!!!!!!!");
- LOG.E ("Hellobroadreciever", Intent.getstringextra ("Yaoyao"));
- }
- Play a piece of music
- Mediaplayer.create (context, R.raw.babayetu). Start ();
- }
- }
Copy Code
6. Change Androidmanifest.xml content as follows:
Java code:
- <?xml version= "1.0" encoding= "Utf-8"?>
- <manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "Android.basic.lesson21" Android: Versionname= "1.0" android:versioncode= "1" >
- <application android:icon= "@drawable/icon" android:label= "@string/app_name" >
- <activity android:label= "@string/app_name" Android:name= ". Mainbroadcastreceiver ">
- <intent-filter= "" >
- <action android:name= "Android.intent.action.MAIN" >
- <category android:name= "Android.intent.category.LAUNCHER" >
- </category></action></intent>
- </activity>
- <!--define broadcast receiver to specify the action of the listener here our receiver, received 2 action, one of the system we customized--
- <receiver android:name= "Hellobroadreciever" >
- <intent-filter= "" >
- <action android:name= "Android.intent.action.BOOT_COMPLETED" >
- </action></intent>
- <intent-filter= "" >
- <action android:name= "Android.basic.lesson21.HelloYaoYao" >
- </action></intent>
- </receiver>
- </application>
- <uses-sdk= "" android:minsdkversion= "8" >
- </uses></manifest>
Copy Code
<ignore_js_op>
<ignore_js_op>