3

I have an app which runs a background service overnight. It's triggered to run by an alarm clock app.

The app spends the night uploading data off the phone's external SD card onto Dropbox. It worked seamlessly on previous versions of Android but is not working on Pie. The background service is killed after running for about two hours every night. Interestingly, however, I've noticed that if I make a tiny change to my code, e.g. editing a string, and then run a debug, the app runs perfectly the next night but then on subsequent nights, goes back to being killed after two hours.

I've tried the following:

  • Using a foreground service with a persistent notification
  • Opening and closing an Activity after the app is opened so it's in the recent apps list
  • Making the app a device administrator
  • Disabling battery optimisations for the app
  • CPU and Wifi wakelocks
  • Running a thread with an infinite loop that uses root privileges to adjust the app's minfree values every five seconds
  • Disabling Pie's adaptive battery manager feature during the night

Despite all of these mechanisms, the app still gets killed. My theory is that there's some kind of artificial intelligent battery manager/performance optimiser on the phone that picks up that the app runs all night and decides to kill it in the future but then gets reset when I re-install the app.

I've tried everything and I still can't seem to find a solution. Can someone please point me in the right direction? I'm sure there's some root/reflection thing that I can do to fix this but I just don't know what it is!

3 Answers3

1

I found the problem! Turns out my phone had a service called G3 which was killing the app to "save power". Apparently, this service is useless so I uninstalled it and the problem was solved instantly!

I used the following command:

adb shell pm uninstall --user 0 com.evenwell.powersaving.g3
adb shell pm uninstall --user 0 com.evenwell.powersaving.g3.overlay.base.s600ww

Pretty annoyed that this service took to killing an app that had root, administrator privileges and permission to avoid battery optimisations - how obvious do I have to make it that I want the app to stay alive?!

0

try job schedular

https://developer.android.com/reference/android/app/job/JobScheduler.html

https://developer.android.com/topic/performance/scheduling.html

use Alarm manager

https://developer.android.com/reference/android/app/AlarmManager.html

As one of the changes that Android 8.0 (API level 26) introduces to improve battery life, when your app enters the cached state, with no active components, the system releases any wakelocks that the app holds.

In addition, to improve device performance, the system limits certain behaviors by apps that are not running in the foreground. Specifically:

Apps that are running in the background now have limits on how freely they can access background services. Apps cannot use their manifests to register for most implicit broadcasts (that is, broadcasts that are not targeted specifically at the app). By default, these restrictions only apply to apps that target O. However, users can enable these restrictions for any app from the Settings screen, even if the app has not targetted O.

Praveen
  • 946
  • 6
  • 14
  • Thanks for the help Praveen. This is how I originally made the alarm clock app work but the alarm clock isn't the problem. The backup app starts up fine because the alarm clock app triggers it (see the link) so the alarm manager and job scheduler aren't needed. The problem was that when the app was running, Android was killing it. I've managed to solve it now by uninstalling an aggressive stock power manager from the phone. – Ben Ramcharan Feb 28 '19 at 21:29
0

Nothing will work like job schedular or Alarm manager

Your problem will be only resolved by using WorkManager

Sandip Savaliya
  • 784
  • 6
  • 19
  • 1
    Thanks ShockWave. I did try the WorkManager at one point but it caused more problems than it solved (sorry, forgot to mention this in the question). The app was still killed when using the WorkManager. Turns out it was an aggressive power manager app that was killing my app so even the WorkManager wasn't able to help. – Ben Ramcharan Feb 28 '19 at 21:32