1

How can I get sure that a foreground service can use CPU when app is closed, when screen is locked and when doze mode is active?

There are handlerthreads in which I plan tasks with postdelayed method within the foreground service. I list my test cases:

  1. Partial wakelock in foreground service:
    Handlers never delay.
  2. No wakelock, device is connected through adb:
    Handlers never delay.(doze mode, background case, screen lock all tried.)
  3. No wakelock, app is on the screen:
    Handlers never delay.
  4. No wakelock, app on the background, app not destroyed:
    Handlers may delay.
  5. No wakelock, screen on, app destroyed:
    Handlers may delay.
  6. No wakelock, screen locked:
    Handlers always delay unknowningly.
  7. No wakelock, doze active:
    Handlers always delay unknowningly.

Documentations don't say much about foreground services cpu usage limitations. Are there bugs related to foreground services?

Mertcan Çüçen
  • 466
  • 3
  • 13

1 Answers1

1

The handler's postDelayed() is behaving as expected. Based on documentation of postDelayed():

Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached. The time-base is uptimeMillis(). Time spent in deep sleep will add an additional delay to execution

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • Thanks for pointing this. Now, I use Timer and Timertasks inside the foreground service but it still doesnt trigger on time when phone goes into dose mode. How can I get sure my foreground service can use cpu at wish? – Mertcan Çüçen Apr 16 '18 at 10:56
  • If you wish your service should be able to utilize the CPU during sleep mode, you can acquire [PartialWakeLock](https://developer.android.com/reference/android/os/PowerManager.html#PARTIAL_WAKE_LOCK) which ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off. – Sagar Apr 17 '18 at 00:41