本地广播较之普通的广播更加安全,发出的广播只能在当前APP中能够接收到,其他APP无法接收。
Intent intent = new Intent();
intent.setAction(Constants.ANSWERED_RECEIVER_ACTION);//自定义action
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
private class AnsweredReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
postPaperRecord(examPaperBean);
LocalBroadcastManager.getInstance(context).unregisterReceiver(answeredReceiver);
}
}
answeredReceiver = new AnsweredReceiver();
IntentFilter filter = new IntentFilter(Constants.ANSWERED_RECEIVER_ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(answeredReceiver,filter);
LocalBroadcastManager.getInstance(context).unregisterReceiver(answeredReceiver);
使用方式跟普通的广播类似,看一遍基本就会用了。
如果有疑问给我留言,会尽快回复。
欢迎参观博主的其他博客。