TextView content;
Button btn;
public final static String ACTION = "life";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.brocast);
content=(TextView) findViewById(R.id.content);
btn=(Button) findViewById(R.id.button);
registerBroadcastReceiver();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,B.class));
}
});
}
private void registerBroadcastReceiver(){
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION);
registerReceiver(mReceiver, filter);
}
private BroadcastReceiver mReceiver = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(ACTION)){
// page = 1;
// commentAdapter.clear();
// lifeInfo.commentList.clear();
// commentAdapter.notifyDataSetChanged();
// requestLifeData();
Log.i("0126",intent.getStringExtra("brocast") );
content.setText(intent.getStringExtra("brocast"));
}
};
};
MainActivity注册广播,B发送广播,MainActivity能接受B发来的广播
public class B extends Activity {
Button to_a;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.brocast_b);
to_a=(Button) findViewById(R.id.to_a);
to_a.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it = new Intent(MainActivity.ACTION);
it.putExtra("brocast", "从B来的的广播");
sendBroadcast(it);
finish();
}
});
}
}