skip to Main Content

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

Answers


  1. Chosen as BEST ANSWER

    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?!


  2. 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.

    Login or Signup to reply.
  3. Nothing will work like job schedular or Alarm manager

    Your problem will be only resolved by using WorkManager

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search