码迷,mamicode.com
首页 > 移动开发 > 详细

android 广播的使用

时间:2014-09-09 21:39:39      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   os   io   使用   java   

在Activity中,注册广播的一个Demo。

总共分3步

第一步:定义一个BroadcastReceiver广播接收类:

[java] view plaincopy
  1. private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){  
  2.         @Override  
  3.         public void onReceive(Context context, Intent intent) {  
  4.             String action = intent.getAction();  
  5.             if(action.equals(ACTION_NAME)){  
  6.                 Toast.makeText(Test.this"处理action名字相对应的广播"200);  
  7.             }  
  8.         }  
  9.           
  10.     };  


 

第二步:注册该广播:

[java] view plaincopy
  1. public void registerBoradcastReceiver(){  
  2.         IntentFilter myIntentFilter = new IntentFilter();  
  3.         myIntentFilter.addAction(ACTION_NAME);  
  4.         //注册广播        
  5.         registerReceiver(mBroadcastReceiver, myIntentFilter);  
  6.     }  


 

第三步:触发响应

 

[java] view plaincopy
  1. mBtnMsgEvent = new Button(this);  
  2.         mBtnMsgEvent.setText("发送广播");  
  3.         mBtnMsgEvent.setOnClickListener(new OnClickListener() {  
  4.             @Override  
  5.             public void onClick(View v) {  
  6.                 Intent mIntent = new Intent(ACTION_NAME);  
  7.                 mIntent.putExtra("yaner""发送广播,相当于在这里传送数据");  
  8.                   
  9.                 //发送广播  
  10.                 sendBroadcast(mIntent);  
  11.             }  
  12.         });  
  13.       


 

 

-----最后附上完整代码:

[java] view plaincopy
  1. package my.yaner;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.BroadcastReceiver;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.IntentFilter;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12. import android.widget.LinearLayout;  
  13. import android.widget.Toast;  
  14.   
  15. public class Test extends Activity{  
  16.     private final String ACTION_NAME = "发送广播";  
  17.     private Button mBtnMsgEvent = null;  
  18.       
  19.     protected void onCreate(Bundle savedInstanceState){  
  20.         super.onCreate(savedInstanceState);  
  21.           
  22.         //注册广播  
  23.         registerBoradcastReceiver();  
  24.           
  25.         LinearLayout mLinearLayout = new LinearLayout(this);  
  26.         mBtnMsgEvent = new Button(this);  
  27.         mBtnMsgEvent.setText("发送广播");  
  28.         mLinearLayout.addView(mBtnMsgEvent);  
  29.         setContentView(mLinearLayout);  
  30.           
  31.         mBtnMsgEvent.setOnClickListener(new OnClickListener() {  
  32.             @Override  
  33.             public void onClick(View v) {  
  34.                 Intent mIntent = new Intent(ACTION_NAME);  
  35.                 mIntent.putExtra("yaner""发送广播,相当于在这里传送数据");  
  36.                   
  37.                 //发送广播  
  38.                 sendBroadcast(mIntent);  
  39.             }  
  40.         });  
  41.     }  
  42.       
  43.     private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){  
  44.         @Override  
  45.         public void onReceive(Context context, Intent intent) {  
  46.             String action = intent.getAction();  
  47.             if(action.equals(ACTION_NAME)){  
  48.                 Toast.makeText(Test.this"处理action名字相对应的广播"200);  
  49.             }  
  50.         }  
  51.           
  52.     };  
  53.       
  54.     public void registerBoradcastReceiver(){  
  55.         IntentFilter myIntentFilter = new IntentFilter();  
  56.         myIntentFilter.addAction(ACTION_NAME);  
  57.         //注册广播        
  58.         registerReceiver(mBroadcastReceiver, myIntentFilter);  
  59.     }  
  60. }  

android 广播的使用

标签:android   style   blog   http   color   os   io   使用   java   

原文地址:http://blog.csdn.net/lsong89/article/details/39160427

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!