LocalBroadcastManager mLocalBroadcastManager;
BroadcastReceiver mReceiver;
//注册通知
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_UPDATE); mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_UPDATE)) {
callbackData.setText("Got update: " + intent.getIntExtra("value", 0));
}
}
};
mLocalBroadcastManager.registerReceiver(mReceiver, filter);
//发送通知
mLocalBroadcastManager.sendBroadcast(new Intent(ACTION_UPDATE));
//注销通知
mLocalBroadcastManager.unregisterReceiver(mReceiver);
仅接收本应用发送的通知,比较安全(替代BroadcastManager使用)