Use setExact for long-interval recording

This commit is contained in:
sonora 2019-05-04 19:53:09 +02:00
parent 0fc88a77b7
commit fd05a1f058
2 changed files with 51 additions and 2 deletions

View file

@ -10,6 +10,7 @@ import android.location.Location;
import android.location.LocationListener; import android.location.LocationListener;
import android.location.LocationManager; import android.location.LocationManager;
import android.os.Binder; import android.os.Binder;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
@ -41,6 +42,7 @@ public class NavigationService extends Service implements LocationListener {
private int serviceOffInterval; private int serviceOffInterval;
private String serviceOffProvider; private String serviceOffProvider;
private int serviceError; private int serviceError;
private long nextManualWakeup;
private OsmandSettings settings; private OsmandSettings settings;
private Handler handler; private Handler handler;
@ -71,6 +73,14 @@ public class NavigationService extends Service implements LocationListener {
return serviceError; return serviceError;
} }
public long getNextManualWakeup() {
return nextManualWakeup;
}
public void setNextManualWakeup(long value) {
nextManualWakeup = value;
}
public int getServiceOffInterval() { public int getServiceOffInterval() {
return serviceOffInterval; return serviceOffInterval;
} }
@ -111,8 +121,15 @@ public class NavigationService extends Service implements LocationListener {
app.setNavigationService(this); app.setNavigationService(this);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, OnNavigationServiceAlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT); pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, OnNavigationServiceAlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
nextManualWakeup = SystemClock.elapsedRealtime() + serviceOffInterval;
if (Build.VERSION.SDK_INT >= 23) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, pendingIntent);
} else if (Build.VERSION.SDK_INT >= 19) {
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, pendingIntent);
} else {
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, serviceOffInterval, pendingIntent); alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, serviceOffInterval, pendingIntent);
} }
}
app.getNotificationHelper().updateTopNotification(); app.getNotificationHelper().updateTopNotification();
app.getNotificationHelper().refreshNotifications(); app.getNotificationHelper().refreshNotifications();
@ -159,8 +176,15 @@ public class NavigationService extends Service implements LocationListener {
} else { } else {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, OnNavigationServiceAlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT); pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, OnNavigationServiceAlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
nextManualWakeup = SystemClock.elapsedRealtime() + serviceOffInterval;
if (Build.VERSION.SDK_INT >= 23) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, pendingIntent);
} else if (Build.VERSION.SDK_INT >= 19) {
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, pendingIntent);
} else {
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, serviceOffInterval, pendingIntent); alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, serviceOffInterval, pendingIntent);
} }
}
// registering icon at top level // registering icon at top level
// Leave icon visible even for navigation for proper display // Leave icon visible even for navigation for proper display

View file

@ -1,10 +1,15 @@
package net.osmand.plus; package net.osmand.plus;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.location.LocationManager; import android.location.LocationManager;
import android.os.Build;
import android.os.PowerManager.WakeLock; import android.os.PowerManager.WakeLock;
import android.os.SystemClock;
public class OnNavigationServiceAlarmReceiver extends BroadcastReceiver { public class OnNavigationServiceAlarmReceiver extends BroadcastReceiver {
@Override @Override
@ -19,6 +24,26 @@ public class OnNavigationServiceAlarmReceiver extends BroadcastReceiver {
lock.acquire(); lock.acquire();
// request location updates // request location updates
final LocationManager locationManager = (LocationManager) service.getSystemService(Context.LOCATION_SERVICE); final LocationManager locationManager = (LocationManager) service.getSystemService(Context.LOCATION_SERVICE);
//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);
}
try { try {
locationManager.requestLocationUpdates(service.getServiceOffProvider(), 0, 0, service); locationManager.requestLocationUpdates(service.getServiceOffProvider(), 0, 0, service);
if (service.getServiceOffInterval() > service.getServiceError()) { if (service.getServiceOffInterval() > service.getServiceError()) {