发送广播:
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;
}
}