I have developed a busy app used for GPS tracking. It works well while in the foreground. It uses GPS/location callbacks along with background services for Text-to-Speech, messages and Heart Rate Monitor hardware. The problem is that Android OS occasionally shuts it down when running in the background. If the screen is closed using the POWER button it keeps running OK. If the HOME button is used to close the screen, and run other apps, Android sometimes shuts the app down after a few minutes, even though it needs to keep working. Is there a way to keep the app running using isFinishing()/onDestroy() or similar? The BACK button operation is captured correctly giving the user an option to cancel. Thanks, Chris.
Asked
Active
Viewed 1,777 times
0
-
1Use Sticky Service for Updating in Background. Although you have to look into Android Doze behavior changes since Android M, here is for [Android N](https://developer.android.com/about/versions/nougat/android-7.0-changes.html),[Android O](https://developer.android.com/about/versions/oreo/background-location-limits.html). – ADM Oct 16 '17 at 10:11
1 Answers
2
To avoid this your app should start a foreground Service (a Service that calls startForeground() ) , show a non-dismissable notification and acquire a PowerManager.PARTIAL_WAKE_LOCK
It prevents the phone to enter sleep mode and prevents the system from killing your app (it can not be avoided at 100%), but this will obviously have a significant effect on battery consumption.

from56
- 3,976
- 2
- 13
- 23
-
1"It prevents the phone to enter sleep mode" -- no, it does not. A `WakeLock` prevents the device from going into sleep mode. A foreground service does not automatically acquire a `WakeLock`. – CommonsWare Oct 16 '17 at 11:10
-
1Ok, I forgot the Wake Lock ... edited. But in some tests that I did a few months ago it worked the same with and without wake_lock but I still kept it, just in case. – from56 Oct 16 '17 at 11:21