本文主要是介绍Android 发送广播传送数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
发送广播:
Intent intent = new Intent(); intent.putExtra("status", status.getId()); intent.putExtra("myClass", myClass.getId()); intent.putExtra("order", order.getId()); intent.setAction("com.emay.power.MyBroadCastReceiver"); BroadcastUtil.getMyLocalBroadcast(getActivity()).sendBroadcast( intent);
接收广播:
try {
IntentFilter filter = new IntentFilter();
filter.addAction("com.emay.power.MyBroadCastReceiver");
BroadcastUtil.getMyLocalBroadcast(getActivity()).registerReceiver(
new LocalReceiver(), filter);
} catch (Exception e) {
e.printStackTrace();
}
广播接收类:
public class LocalReceiver extends BroadcastReceiver { @Override public void onReceive(Context arg0, Intent tent) { status = tent.getStringExtra("status"); myClass = tent.getStringExtra("myClass"); scoreType = tent.getStringExtra("order"); show = true; loadData(); } }
工具类:
public class BroadcastUtil {
private static LocalBroadcastManager localBroadcastManager = null;
public static LocalBroadcastManager getMyLocalBroadcast(Context context) {
if (null == localBroadcastManager) {
localBroadcastManager = LocalBroadcastManager.getInstance(context);
}
return localBroadcastManager;
}
}
这篇关于Android 发送广播传送数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!