我有一个片段TextView
。从我的应用程序的不同位置我想更新它TextView
,所以我正在使用LocalBroadcastManager
但它并不总是有效 - 事实上,当我禁用设备管理员(这是 MDM 应用程序)时,它“忘记”接收数据......
public static void sendMessage(String message) {
MESSAGE = message;
Intent intent = new Intent(MESSAGE_INTENT_FILTER);
intent.putExtra(MESSAGE_EXTRA, message);
App.getAppContext().sendBroadcast(intent);
}
App.getAppContext()
是在我的 Application 类中声明的静态上下文。
@Override
public void onResume() {
super.onResume();
updateStatus(MESSAGE);
}
在onResume()
我更新存储的消息时(因为我的片段并不总是出现。
我的接收者:
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra(MESSAGE_EXTRA);
updateStatus(message);
}
};
onStart / onStop:
@Override
public void onStart() {
super.onStart();
initReceiver();
IntentFilter intentFilter = new IntentFilter(MESSAGE_INTENT_FILTER);
LocalBroadcastManager.getInstance(App.getAppContext()).registerReceiver(receiver, intentFilter);
}
@Override
public void onStop() {
super.onStop();
LocalBroadcastManager.getInstance(App.getAppContext()).unregisterReceiver(receiver);
}
我的日志:
在开始片段上
在简历片段上 - 消息:消息 1
更新状态 - 消息:消息 1
发送消息:消息 2
停止片段
在开始片段上
在简历片段上 - 消息:消息 2
更新状态 - 消息:消息 2
禁用管理员
发送消息:取消
就是这样......不再接收