Can anyone describe or point me to a good description to "how wakelock in android works"? You can forget Android standard documentation for it.
I have a service (started as foreground) and in the service an own thread will be started. The thread remains in a loop until the service will be stopped by the user. In the loop, the thread performs a HTTP request. From the response the thread knows after what delay (1 minute or greater) the next HTTP request shall be performed.
When the display is on, then the HTTP requests reach the server in the exact expected time. But when the display is off, the delay is somehow random. E.g. in case of 1 minute in fact its 2:21, 4:04, 4:40, ... But, I want the exact timing also in case of display off (assuming to be called deep-sleep mode).
So I introduced a wakelock.acquire()
before and wakelock.release
after the HTTP request, but this changes nothing.
But it gives me just general questions:
How the hell shall my code wake up to do the wakelock.acquire()
when the phone sleeps? If I reach the wakelock.acquire()
, then I do not need it, or ...?
Ok then I thought, maybe in deep sleep my code is also performed, but certain things like IP traffic is not. But then it should work.
From the logcat logs I can see that wakelock.acquire()
is not called in time. So what is the usage of it? I can not perform the wakelock.acquire()
when starting the service or its thread, because that would mean my service will prevent the phone to go sleep even when I just want to make the HTTP request in 30 minutes time frame.
I rather can't believe that with wakelock.acquire()
you can only make a service for its whole lifetime, preventing the phone from deep-sleep.