Android broadcast explorer Programming

Source: Internet
Author: User

In Android, broadcast is a very useful function. Broadcast can notify other broadcasts to accept this event. Such as insufficient power supply and poor signal.

Below I made a simple demo, first look at the activity

 
 
  1. package com.android.broadcasttest; 
  2.  
  3. import android.app.Activity; 
  4. import android.content.Intent; 
  5. import android.os.Bundle; 
  6. import android.view.View; 
  7. import android.view.View.OnClickListener; 
  8. import android.widget.Button; 
  9.  
  10. public class BroadcastTest extends Activity { 
  11.     public static final String NEW_LIFEFORM_DETECTED =  
  12.         "com.android.broadcasttest.NEW_LIFEFORM"; 
  13.      
  14.     /** Called when the activity is first created. */ 
  15.     @Override 
  16.     public void onCreate(Bundle savedInstanceState) { 
  17.         super.onCreate(savedInstanceState); 
  18.         setContentView(R.layout.main); 
  19.          
  20.         Button btn0 = (Button)findViewById(R.id.btn0); 
  21.         btn0.setOnClickListener(new OnClickListener() { 
  22.             public void onClick(View v) { 
  23.                 Intent it = new Intent(NEW_LIFEFORM_DETECTED);           
  24.                 sendBroadcast(it); 
  25.             } 
  26.         }); 
  27.     } 

A button is generated in this activity. When the button is pressed, a broadcast is sent through sendbroadcast.

Let's look at the broadcast receiver code:

 
 
  1. Package com. Android. broadcasttest;
  2.  
  3. Import Android. content. broadcastreceiver;
  4. Import Android. content. context;
  5. Import Android. content. intent;
  6. Import Android. util. log;
  7. Import Android. widget. Toast;
  8.  
  9. Public class mybroadcastreceiver extends broadcastreceiver {
  10. Public static final string burn =
  11. "Com. paad. Alien. Action. burn_it_with_fire ";
  12. Public mybroadcastreceiver (){
  13. Log. V ("broadcast_tag", "mybroadcast ");
  14. }
  15. @ Override
  16. Public void onreceive (context, intent ){
  17. // Todo auto-generated method stub
  18. Toast. maketext (context, "successfully received broadcast:", Toast. length_long). Show ();
  19. }
  20.  
  21. }

In onreceive (), an action is performed when the broadcast is received.

We also need to register the handler in androidmanifest. xml

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.       package="com.android.broadcasttest" 
  4.       android:versionCode="1" 
  5.       android:versionName="1.0"> 
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name"> 
  7.         <activity android:name=".BroadcastTest" 
  8.                   android:label="@string/app_name"> 
  9.             <intent-filter> 
  10.                 <action android:name="android.intent.action.MAIN" /> 
  11.                 <category android:name="android.intent.category.LAUNCHER" /> 
  12.             </intent-filter> 
  13.         </activity> 
  14.         <receiver android:name=".MyBroadcastReceiver"> 
  15.             <intent-filter> 
  16.                 <action android:name="com.android.broadcasttest.NEW_LIFEFORM" /> 
  17.             </intent-filter> 
  18.         </receiver> 
  19.     </application>         
  20.     <uses-sdk android:minSdkVersion="8" /> 
  21. </manifest>  

The operator's Action defines the broadcast that the receiver can accept.

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.