Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreground app killed by OS after ~ 1 hour in standby mode

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)

like image 287
Rashad.Z Avatar asked Oct 04 '18 13:10

Rashad.Z


People also ask

Can foreground service be killed by Android?

The Android system stops a service only when memory is low and it must recover system resources for the activity that has user focus. If the service is bound to an activity that has user focus, it's less likely to be killed; if the service is declared to run in the foreground, it's rarely killed.

What does it mean to have an app running in the foreground?

Running in the Foreground means your app is currently Fully Visible on your device, you can see it and interact with it and it will respond to you right away. Follow this answer to receive notifications.

Which method is called when app is killed?

When Android decides to kill our app, our activities will call onDestroy method.


1 Answers

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
like image 99
Andrea Scalabrini Avatar answered Nov 07 '22 01:11

Andrea Scalabrini