Getting Started with Android broadcast Receiver

Source: Internet
Author: User

     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:

    1. Package Android.demo;
    2. Import Android.content.BroadcastReceiver;
    3. Import Android.content.Context;
    4. Import android.content.Intent;
    5. Import Android.media.MediaPlayer;
    6. Import Android.util.Log;
    7. public class Hellobroadreciever extends Broadcastreceiver {
    8. If the received event occurs
    9. @Override
    10. public void OnReceive (context context, Intent Intent) {
    11. The output log
    12. LOG.E ("Hellobroadreciever", "boot_completed!!!!!!!!!!!!!!!!!!!!!!!!!");
    13. LOG.E ("Hellobroadreciever", "" +intent.getaction ());
    14. Then play a piece of music
    15. Mediaplayer.create (context, R.raw.babayetu). Start ();
    16. }
    17. }
Copy Code


3. Register this receiver in Androidmanifest.xml:

Java code:

  1. <?xml version= "1.0" encoding= "Utf-8"?>
  2. <manifest xmlns:android= "http://schemas.android.com/apk/res/android" Android:versionname= "1.0" Android: Versioncode= "1" package= "Android.basic.lesson21" >
  3. <application android:icon= "@drawable/icon" android:label= "@string/app_name" >
  4. <activity android:label= "@string/app_name" Android:name= ". Mainbroadcastreceiver ">
  5. <intent-filter= "" >
  6. <action android:name= "Android.intent.action.MAIN" >
  7. <category android:name= "Android.intent.category.LAUNCHER" >
  8. </category></action></intent>
  9. </activity>
  10. <!--define broadcast Receiver to specify the action of the listener--
  11. <receiver android:name= "Hellobroadreciever" >
  12. <intent-filter= "" >
  13. <action android:name= "Android.intent.action.BOOT_COMPLETED" >
  14. </action></intent>
  15. </receiver>
  16. </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:

  1. Package Android.demo;
  2. Import android.app.Activity;
  3. Import android.content.Intent;
  4. Import Android.os.Bundle;
  5. Import Android.view.View;
  6. Import Android.widget.Button;
  7. public class Mainbroadcastreceiver extends Activity {
  8. /** called when the activity is first created. */
  9. @Override
  10. public void OnCreate (Bundle savedinstancestate) {
  11. Super.oncreate (savedinstancestate);
  12. Setcontentview (R.layout.main);
  13. Button B1 = (button) Findviewbyid (R.ID.BUTTON01);
  14. B1.setonclicklistener (New View.onclicklistener () {
  15. @Override
  16. public void OnClick (View v) {
  17. Define a intent
  18. Intent Intent = new Intent (). Setaction (
  19. "Android.basic.lesson21.Hello"). PutExtra ("Yaoyao",
  20. "Yaoyao is 189 days old, weeks-2010-08-10");
  21. Broadcast out
  22. Sendbroadcast (Intent);
  23. }
  24. });
  25. }
  26. }
Copy Code


5. Change Hellobroadreceiver.java content as follows:

Java code:

  1. Package Android.demo;
  2. Import Android.content.BroadcastReceiver;
  3. Import Android.content.Context;
  4. Import android.content.Intent;
  5. Import Android.media.MediaPlayer;
  6. Import Android.util.Log;
  7. public class Hellobroadreciever extends Broadcastreceiver {
  8. If the received event occurs
  9. @Override
  10. public void OnReceive (context context, Intent Intent) {
  11. Compare action to decide what information to output
  12. if (Intent.getaction (). Equals ("Android.intent.action.BOOT_COMPLETED")) {
  13. LOG.E ("Hellobroadreciever", "boot_completed!!!!!!!!!!!!!!!!!!!!!!!!!");
  14. }
  15. if (Intent.getaction (). Equals ("Android.basic.lesson21.Hello")) {
  16. LOG.E ("Hellobroadreciever", "Say Hello to Yaoyao!!!!!!!!!!!!!!!!!!!!!!!!!");
  17. LOG.E ("Hellobroadreciever", Intent.getstringextra ("Yaoyao"));
  18. }
  19. Play a piece of music
  20. Mediaplayer.create (context, R.raw.babayetu). Start ();
  21. }
  22. }
Copy Code


6. Change Androidmanifest.xml content as follows:

Java code:

  1. <?xml version= "1.0" encoding= "Utf-8"?>
  2. <manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "Android.basic.lesson21" Android: Versionname= "1.0" android:versioncode= "1" >
  3. <application android:icon= "@drawable/icon" android:label= "@string/app_name" >
  4. <activity android:label= "@string/app_name" Android:name= ". Mainbroadcastreceiver ">
  5. <intent-filter= "" >
  6. <action android:name= "Android.intent.action.MAIN" >
  7. <category android:name= "Android.intent.category.LAUNCHER" >
  8. </category></action></intent>
  9. </activity>
  10. <!--define broadcast receiver to specify the action of the listener here our receiver, received 2 action, one of the system we customized--
  11. <receiver android:name= "Hellobroadreciever" >
  12. <intent-filter= "" >
  13. <action android:name= "Android.intent.action.BOOT_COMPLETED" >
  14. </action></intent>
  15. <intent-filter= "" >
  16. <action android:name= "Android.basic.lesson21.HelloYaoYao" >
  17. </action></intent>
  18. </receiver>
  19. </application>
  20. <uses-sdk= "" android:minsdkversion= "8" >
  21. </uses></manifest>
Copy Code

<ignore_js_op>

<ignore_js_op>

Related Article

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.