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
[...] An unusual and fundamental feature of Android is that an application
process's lifetime is not directly controlled by the application
itself. Instead, it is determined by the system through a combination
of the parts of the application that the system knows are running, how
important these things are to the user, and how much overall memory is
available in the system.
Foreground Service should be at the top of the importance hierarchy, that determine which processes should be killed when low on memory.
However,
[...] Services that have been running for a long time (such as 30
minutes or more) may be demoted in importance to allow their process
to drop to the cached LRU list described next. This helps avoid
situations where very long running services with memory leaks or other
problems consume so much RAM that they prevent the system from making
effective use of cached processes.
so you can not be sure that the process is not killed by the operating system.
Some precautions that works for me:
- add the application to the list of "protected app" (available in some phones like Huaweii).
- limit the use of resources in the foreground service. For example a process that performs a periodic bluetooth scan is much less likely to be killed than a process that uses gps intensively.
- avoid sending too many notifications to the user and above all do not use PowerManager.Wake Lock
If you have an active service you should not have this problem.
See here.
A foreground process is one that is required for what the user is
currently doing. Various application components can cause its
containing process to be considered foreground in different ways. A
process is considered to be in the foreground if any of the following
conditions hold:
It is running an Activity at the top of the screen that the user is interacting with (its onResume() method has been called).
It has a BroadcastReceiver that is currently running (its BroadcastReceiver.onReceive() method is executing).
It has a Service that is currently executing code in one of its callbacks (Service.onCreate(), Service.onStart(), or
Service.onDestroy()).
There will only ever be a few such processes in the system, and these
will only be killed as a last resort if memory is so low that not even
these processes can continue to run. Generally, at this point, the
device has reached a memory paging state, so this action is required
in order to keep the user interface responsive.
Take a look at this answer.
To detect at the time of killing if you are in the foreground.