7

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)

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
Rashad.Z
  • 2,494
  • 2
  • 27
  • 58
  • Go through it https://forums.androidcentral.com/samsung-galaxy-s5/574211-android-silently-killing-my-apps-how-do-i-stop.html – Jitesh Mohite Oct 20 '18 at 11:10
  • Also, please mention your device and device version – Jitesh Mohite Oct 20 '18 at 13:05
  • Use **`startForegroundService`** for API 26 & above, check more from here, https://developer.android.com/reference/android/content/Context#startForegroundService(android.content.Intent) – Jeel Vankhede Oct 22 '18 at 04:30
  • @JeelVankhede i already tried using this which has the same behavior since i am stating as mentioned in the above code startForground in oncreate of the service – Rashad.Z Oct 22 '18 at 16:59
  • @jiteshmohite the devices i tested on are Huawai Y7 price and samsung s8 – Rashad.Z Oct 22 '18 at 17:00

2 Answers2

7

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
Andrea Scalabrini
  • 1,442
  • 2
  • 18
  • 25
  • thanks for the answer. the service i use is retrieving GPS & Network locations periodically so i guess this is some heavy work which the OS is killing after some time. but what confuses me is that a music application is not destroyed by the OS even after an hour of being in the background. Isn't this also considered heavy work? – Rashad.Z Oct 22 '18 at 14:32
  • 1
    Unfortunately I do not have an answer. I worked on an application that used both bluetooth and gps. Disabling localization features the service was never killed. Otherwise, enabling the GPS and disabling the Bluetooth scan, the service was killed after a few hours. I think that the use of GPS has a negative impact on the classification of the service. I found this behavior only in some devices (ex: Huaweii, Xiaomi). In others (ex: Samsung, Nexus) the service has always worked correctly. – Andrea Scalabrini Oct 22 '18 at 15:09
0

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.

Itamar Kerbel
  • 2,508
  • 1
  • 22
  • 29
  • i already did this and also added logging on destroy function of the activity which is never entered when this behavior happens (ofcourse it enters in normal case when i destroy the app) – Rashad.Z Oct 04 '18 at 13:17
  • Does this happen on a specific phone or on more than 1 phone? Can you monitor and log the phone memory usage to see what is the state of the phone right before you are killed? – Itamar Kerbel Oct 04 '18 at 13:33
  • yes will do. I will monitor the memory usage using the profiler. – Rashad.Z Oct 04 '18 at 13:38