always re-schedule alarm

This commit is contained in:
sonora 2019-05-06 22:48:00 +02:00
parent d11d1f94b8
commit 230d6ef4d7

View file

@ -16,31 +16,18 @@ public class OnNavigationServiceAlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
final WakeLock lock = NavigationService.getLock(context);
final NavigationService service = ((OsmandApplication) context.getApplicationContext()).getNavigationService();
// do not do nothing
if (lock.isHeld() || service == null) {
if (service == null) {
return;
} else if (lock.isHeld()) {
rescheduleAlarm(context);
return;
}
//
lock.acquire();
//Unless setRepeating was used, manually re-schedule service to the next measurement point in the future
if (Build.VERSION.SDK_INT >= 23) {
// Avoid drift
while ((service.getNextManualWakeup() - SystemClock.elapsedRealtime()) < 0) {
service.setNextManualWakeup(service.getNextManualWakeup() + service.getServiceOffInterval());
}
AlarmManager alarmManager = (AlarmManager) service.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, new Intent(context, OnNavigationServiceAlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, service.getNextManualWakeup(), pendingIntent);
} else if (Build.VERSION.SDK_INT >= 19) {
// Avoid drift
while ((service.getNextManualWakeup() - SystemClock.elapsedRealtime()) < 0) {
service.setNextManualWakeup(service.getNextManualWakeup() + service.getServiceOffInterval());
}
AlarmManager alarmManager = (AlarmManager) service.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, new Intent(context, OnNavigationServiceAlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, service.getNextManualWakeup(), pendingIntent);
}
rescheduleAlarm(context);
// request location updates
final LocationManager locationManager = (LocationManager) service.getSystemService(Context.LOCATION_SERVICE);
@ -63,4 +50,22 @@ public class OnNavigationServiceAlarmReceiver extends BroadcastReceiver {
}
}
}
private void rescheduleAlarm(Context context) {
//Unless setRepeating was used, manually re-schedule service to the next measurement point in the future
if (Build.VERSION.SDK_INT >= 19) {
NavigationService service = ((OsmandApplication) context.getApplicationContext()).getNavigationService();
// Avoid drift
while ((service.getNextManualWakeup() - SystemClock.elapsedRealtime()) < 0) {
service.setNextManualWakeup(service.getNextManualWakeup() + service.getServiceOffInterval());
}
AlarmManager alarmManager = (AlarmManager) service.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, new Intent(context, OnNavigationServiceAlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= 23) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, service.getNextManualWakeup(), pendingIntent);
} else {
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, service.getNextManualWakeup(), pendingIntent);
}
}
}
}