android 广播的使用的(转)

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

总共分3步

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

  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.     };  


 

第二步:注册该广播:

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


 

第三步:触发响应

 

  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.       


 

 

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

    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. }  
posted @ 2013-05-10 14:15  大暴雨  阅读(232)  评论(0编辑  收藏  举报