私のアクティビティでは、BroadcastReceiverトリガーされたときに private があり、数ミリ秒後に 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);
}
メソッドに BroadcastReceiver を登録すると、onStartメソッドが呼び出されていると思いOnResume()ます。の登録も解除BroadcastReceiverしonPauseます。
Activity私の意図は、これがに 10 秒ごとに通知を送信することです。アプリケーションを開始すると、サービスは予定どおり 10 秒ごとにアクティビティに通知します。問題は、アクティビティを離れて戻ってきたときに、activity10 秒ごとに通知を送信するのではなく、ランダムな時間に通知を送信することです。LogCatこのランダム性スパムが 4、6、3、8、6 秒ごとに発生していることがわかります。なぜこのような振る舞いをするのでしょうか。