在我的活动中,我有一个私有的BroadcastReceiver,当触发时,应该在一些ms之后更新UI。在我的活动中我有:
private BroadcastReceiver broadCastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("BroadCastReciever: ", "UpdateCaseList");
update.RefreshCaseList();
}
};此BroadcastReceiver是从Service触发的
@Override
public void onCreate() {
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
}
@Override
public void onStart(Intent intent, int startId) {
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 0);
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
handler.postDelayed(this, 10000); // 10 seconds
sendUpdateToUiThread();
}
};
private void sendUpdateToUiThread() {
sendBroadcast(intent);
}我猜想当我在我的OnResume()方法中注册BroadcastReceiver时会调用onStart方法。我还在onPause中取消了BroadcastReceiver的注册。
我的意图是这应该每10秒向Activity发送一个通知。一旦我启动应用程序,我的服务将按计划每10秒通知一次活动。问题是,当我离开活动并返回时,它不会每10秒向activity发送一次通知,而只是随机发送一次通知。我可以在LogCat中看到这种随机性垃圾邮件每4、6、3、8、6秒发生一次,依此类推。为什么会有这种行为呢?
发布于 2016-07-14 17:35:41
根据postDelayed documentation的说法,在经过毫秒之后,可运行的代码被称为
在深度睡眠中花费的
时间将增加执行的额外延迟。
因此,一些随机性是设计出来的。所以我希望在你的例子中,在之后调用的Runnable会超过10000毫秒。
https://stackoverflow.com/questions/17975987
复制相似问题