1

I have a tracking app that needs to send location updates to a web server every 5 seconds. This works fine as long as the app is awake/visible.

If the device is locked the app is put to sleep after 15 minutes (give or take a second or two).

Adding the app to the "Unmonitored apps" under Battery app power monitor doesn't stop it getting put to sleep either.

I have tried creating a wakelock, ie

pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TrackerTag");
wakeLock.acquire();

but even that does not help. The app still goes to sleep and stops sending updates. I do not need to keep the screen on (hence the PARTIAL_WAKE_LOCK) but I do need the app to stay awake so it can feed GPS coordinates to a server database.

I have added the app to the ignore list for battery optimization list so it isn't that killing it and the above wakelock seems to do nothing.

I also tried creating the service as a foreground service ie Android - implementing startForeground for a service? and that also does not save the app being put to sleep after 15 minutes.

What do I need to do to tell android "leave this app alone no matter what and do not stop it unless I specifically close all and kill it"?

Some1Else
  • 715
  • 11
  • 26

1 Answers1

1

After trying every possible way I could find to stop android killing my app the end "fix" is to restart the main activity every 3 minutes. Because the app is never running for 15 minutes this seems to stop Android killing it.

This is the runnable I use. Put this in the main activity onCreate

h.postDelayed(new Runnable() {
    public void run() {
        runnable = this;
        recreate();
        h.postDelayed(runnable, 3*60*1000);
    }
}, 3*60*1000);

This also restarts the activity when it is paused/hidden. If the app is visible there is a quick flicker as the activity is relaunched, but this is not a problem in my case.

Unless someone can give me a real official solution the above will do for now. My app has been running and updating the web server for over an hour when locked. Perviosuly it would never get past 15 minutes.

Some1Else
  • 715
  • 11
  • 26