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

…衆ロ難τιáo~ 提交于 2019-12-03 03:20:31

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!