BroadcastReceiver と WakefulServiceIntent を使用してアプリケーションのメソッドを 10 分ごとに呼び出すアプリケーションを作成しています。問題は、それを呼び出す場合としない場合があることです。アプリがバックグラウンドで実行されていたり、画面がオフになっている場合でも、10 分ごとにこのメソッドを呼び出す必要があります。私の現在のコードは以下です。タイマーを開始するために AlarmManager を使用すべきではありませんか? AlarmManager は正確ではないと聞いたことがありますが、どこまでも不正確なので、鳴らないのですか?
AlarmManager の開始コード:
Intent intent = new Intent(getContext(), UpdateReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis() + ((2 * 60) * 1000)), ((10 * 60) * 1000), pendingIntent);
BroadcastReceiver クラス:
public class UpdateReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, UpdateService.class);
startWakefulService(context, service);
}
}
WakefulIntentService クラス:
public class UpdateService extends IntentService {
public UpdateService() {
super("UpdateService");
}
@Override
protected void onHandleIntent(Intent intent) {
System.out.println("updated automatically");
OverviewFragment.refresh(getApplicationContext());
UpdateReceiver.completeWakefulIntent(intent);
}
}