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

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(ACTION_NAME)){
Toast.makeText(Test.this, “处理action名字相对应的广播”, 200);
}
}

};  

第二步:注册该广播:

public void registerBoradcastReceiver(){
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction(ACTION_NAME);
//注册广播
registerReceiver(mBroadcastReceiver, myIntentFilter);
}

第三步:触发响应

mBtnMsgEvent = new Button(this);
mBtnMsgEvent.setText(“发送广播”);
mBtnMsgEvent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent mIntent = new Intent(ACTION_NAME);
mIntent.putExtra(“yaner”, “发送广播,相当于在这里传送数据”);

            //发送广播              sendBroadcast(mIntent);          }      });  

—–最后附上完整代码:

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Test extends Activity{
private final String ACTION_NAME = “发送广播”;
private Button mBtnMsgEvent = null;

protected void onCreate(Bundle savedInstanceState){      super.onCreate(savedInstanceState);      //注册广播      registerBoradcastReceiver();      LinearLayout mLinearLayout = new LinearLayout(this);      mBtnMsgEvent = new Button(this);      mBtnMsgEvent.setText("发送广播");      mLinearLayout.addView(mBtnMsgEvent);      setContentView(mLinearLayout);      mBtnMsgEvent.setOnClickListener(new OnClickListener() {          @Override          public void onClick(View v) {              Intent mIntent = new Intent(ACTION_NAME);              mIntent.putExtra("yaner", "发送广播,相当于在这里传送数据");              //发送广播              sendBroadcast(mIntent);          }      });  }  private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){      @Override      public void onReceive(Context context, Intent intent) {          String action = intent.getAction();          if(action.equals(ACTION_NAME)){              Toast.makeText(Test.this, "处理action名字相对应的广播", 200);          }      }  };  public void registerBoradcastReceiver(){      IntentFilter myIntentFilter = new IntentFilter();      myIntentFilter.addAction(ACTION_NAME);      //注册广播            registerReceiver(mBroadcastReceiver, myIntentFilter);  }  

}

更多相关文章

  1. InputStream输入流,读取数据实例
  2. Android api 数据的保存方式
  3. 【Java转Android】30. LitePal操作SQLite数据库
  4. Android SQLite数据库相关操作
  5. android 自带的contacts2.db数据库表结构脚本
  6. android 两个apk之间恭喜数据SharedPreferences
  7. Android Sqlite数据库中判断某个表是否存在的SQL语句
  8. Android JSON数据的解析与封装小Demo
  9. Android中使用SQLite数据库详解

随机推荐

  1. 5分钟将OSC Android源码从Eclipse项目转
  2. 谈谈怎么最快学好android
  3. 零打碎敲学Android(二)—做个拼图游戏吧
  4. Android 文件管理方法
  5. Android中为TextView增加自定义的HTML标
  6. 编写高效的android代码(译文)
  7. Android 应用程序基础(Application Fundam
  8. Android 文档的阅读顺序![转]
  9. android:configChanges配置不当,应用被回
  10. Android遍历API (1) 动画篇——克隆动画A