I have an application where the user is requested to sign in and then presented with an activity. A service is also started on the sign in which uses the location manager to track his current location. everything works perfectly until the application is left in standby mode (screen off an app in background for more than ~ 1 hour)
how can I prevent this?
as I understand, if I have a foreground service running, the OS should not kill the app.. so what am I doing wrong? the OS I am testing on is Oreo
starting the service on sign in:
startService(intent);
the service:
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification notification = updateNotificationContent(); // the service notification
if (notification == null)
stopSelf();
startForeground(id, notification);
}
I added logging on destroy the function of the activity and service to detect when this is happening but the log is never written when this behavior happens (of course it enters in normal case when I destroy the app)
According to Android Processes and Application Lifecycle Documentation
Foreground Service should be at the top of the importance hierarchy, that determine which processes should be killed when low on memory.
However,
so you can not be sure that the process is not killed by the operating system.
Some precautions that works for me:
If you have an active service you should not have this problem.
See here.
Take a look at this answer. To detect at the time of killing if you are in the foreground.