android 广播接收的数据如何用TextView显示
发布网友
发布时间:2024-10-23 18:02
我来回答
共1个回答
热心网友
时间:2024-11-06 09:46
这是我以前做过的一个例子,看看能不能给你帮助,给你参考参考吧。。。。
这里面有三个广播。。。。
public class Test020502Activity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
private TextView tv1;
private TextView tv2;
private TextView tv3;
private BroadcastReceiver broadcastReceiver1 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = getResultExtras(false);
String setedString=bundle.getString("key1");
bundle.putString("key1", setedString+"消息接收者1文字");
setResultExtras(bundle);
tv1.setText(setedString+"消息接收者1文字");
Toast.makeText(context, "接收者1"+intent.getStringExtra("saySome"), Toast.LENGTH_SHORT).show();
//中止广播
//abortBroadcast();
}
};
private BroadcastReceiver broadcastReceiver2= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = getResultExtras(false);
String setedString=bundle.getString("key1");
bundle.putString("key1", setedString+"消息接收者2文字");
setResultExtras(bundle);
tv2.setText(setedString+"消息接收者2文字");
Toast.makeText(context, "接收者2"+intent.getStringExtra("saySome"), Toast.LENGTH_SHORT).show();
}
};
private BroadcastReceiver broadcastReceiver3 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//makeMap:1,true:没有得到,自动生成一个新的Bundle对象。返回新的Bundle对象
// 2,false:没有得到,不自动生成,返回的null
Bundle bundle = getResultExtras(true);
bundle.putString("key1", "消息接收者3文字");
setResultExtras(bundle);
tv3.setText("消息接收者3文字");
Toast.makeText(context, "接收者3"+intent.getStringExtra("saySome"), Toast.LENGTH_SHORT).show();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ToggleButton btnReceive1 = (ToggleButton)findViewById(R.id.btnBroadcastReceive1);
ToggleButton btnReceive2 = (ToggleButton)findViewById(R.id.btnBroadcastReceive2);
ToggleButton btnReceive3 = (ToggleButton)findViewById(R.id.btnBroadcastReceive3);
btnReceive1.setOnClickListener(this);
btnReceive2.setOnClickListener(this);
btnReceive3.setOnClickListener(this);
Button btnSendBroadcast =(Button)findViewById(R.id.btnSendBroadcast);
Button btnSendOrderBroadcast =(Button)findViewById(R.id.btnSendOrderBroadcast);
Button btnSendStickyBroadcast =(Button)findViewById(R.id.btnSendStickyBroadcast);
btnSendStickyBroadcast.setOnClickListener(this);
btnSendBroadcast.setOnClickListener(this);
btnSendOrderBroadcast.setOnClickListener(this);
Button btnStart =(Button)findViewById(R.id.btnStart);
btnStart.setOnClickListener(this);
tv1=(TextView)findViewById(R.id.tv1);
tv2=(TextView)findViewById(R.id.tv2);
tv3=(TextView)findViewById(R.id.tv3);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnBroadcastReceive1:
//消息接收者的注册与解除注册
ToggleButton b1=(ToggleButton)v;
if(b1.isClickable()){//要接收广播,要注册
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.newer.test020502");
intentFilter.setPriority(500);//当前消息接收者的优先级
registerReceiver(broadcastReceiver1, intentFilter);
}else{//不需要接收广播,取消注册
unregisterReceiver(broadcastReceiver1);
}
break;
case R.id.btnBroadcastReceive2:
ToggleButton b2=(ToggleButton)v;
if(b2.isClickable()){//要接收广播,要注册
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.newer.test020502");
intentFilter.setPriority(600);//当前消息接收者的优先级
registerReceiver(broadcastReceiver2, intentFilter);
}else{//不需要接收广播,取消注册
unregisterReceiver(broadcastReceiver2);
}
break;
case R.id.btnBroadcastReceive3:
ToggleButton b3=(ToggleButton)v;
if(b3.isClickable()){//要接收广播,要注册
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.newer.test020502");
intentFilter.setPriority(700);//当前消息接收者的优先级
registerReceiver(broadcastReceiver3, intentFilter);
}else{//不需要接收广播,取消注册
unregisterReceiver(broadcastReceiver3);
}
break;
case R.id.btnSendOrderBroadcast:
//发送排序广播
Intent intent=new Intent();
intent.setAction("com.newer.test020502");
intent.putExtra("saySome", "Ho~~~~~~~La~~~~~~~");
sendOrderedBroadcast(intent, null);
break;
case R.id.btnSendBroadcast:
Intent intent1 = new Intent();
intent1.setAction("com.newer.sendBroadcast");
intent1.setFlags(1);
sendBroadcast(intent1);
break;
case R.id.btnSendStickyBroadcast:
Intent intent2 = new Intent();
intent2.setAction("com.newer.sendStickyBroadcast");
intent2.setFlags(2);
sendStickyBroadcast(intent2);
break;
case R.id.btnStart:
Intent intent3=new Intent(Test020502Activity.this,OtherActivity.class);
startActivity(intent3);
break;
default:
break;
}
}
}