0

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.

lucian.pantelimon
  • 3,673
  • 4
  • 29
  • 46
valerian
  • 23
  • 6
  • Hey, maybe I am totally wrong and with wakelocks I can only prevent a phone going to deep-sleep, but I cannot wakeup a phone from deep-sleep. But isn't it not the normal case, that you need to wakeup the phone for some time to do something??? And the other question is, why is the http request performed at all in deep-sleep. Does my service get some processing time, because some other app wakeup-ed the phone? But how this app is doing it. I want it doing the same. – valerian May 20 '13 at 14:16

1 Answers1

0

In general, wake locks only keep the screen or CPU on. They do not wake up the phone when it is already asleep. You can pass ACQUIRE_CAUSES_WAKEUP when you create a wake lock to change this behaviour, but it will still not do what you want, due to Android's behaviour during deep sleep. Essentially, your call to Wakelock.acquire() will always happen too late, because of the behaviour of Android in sleep mode -- it is only running your code when it feels like it, because it's asleep.

Since you want updates to happen at a very specific time when you're in the background, it looks like you should be using AlarmManager.set along with one of the ELAPSED_REALTIME_WAKEUP or RTC_WAKEUP flags. Use setInexactRepeating() if you can, see http://www.google.com/events/io/2009/sessions/CodingLifeBatteryLife.html for reasons why (battery life).

Logan Pickup
  • 2,294
  • 2
  • 22
  • 29