android Broadcast广播消息代码实现
我用的是Fragment , 发送写在一个类中,接收写在另外一个类的内部类中。代码动态实现注册。
代码:
myReceiver = new zcd.netanything.MyCar.myReceiver(); IntentFilter filter = new IntentFilter(); //向过滤器中添加action filter.addAction("zcd.netanything"); //注册广播 getActivity().registerReceiver(myReceiver, filter); //broadcast Intent intent=new Intent(); intent.putExtra("wifiurl",wifiurl.getText().toString()); intent.putExtra("wificom",wificom.getText().toString()); intent.putExtra("wificamera",wificamera.getText().toString()); intent.setAction("zcd.netanything"); getActivity().sendBroadcast(intent);
博客来源:http://www.cnblogs.com/xiaobo-Linux/ 小波linux qq463431476 (赵存档)
内部类:
//内部类 广播接收 public static class myReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub wifiurl = intent.getStringExtra("wifiurl"); wificom = intent.getStringExtra("wificom"); wificamera = intent.getStringExtra("wificamera"); Toast.makeText(context, "广播已经接收", Toast.LENGTH_SHORT).show(); } }
这样就可以了。