Will a Handler postDelayed not being fired when CPU sleeps?
Asked Answered
M

1

20

I have an activity with some Handlers that are being executed at intervals no more than 5 minutes. The activity is launched from a BroadcastReceiver and may be launched with the screen off waiting for the user to grab the phone and get the user input, when this happens, the activity onPause() is called so for sure CPU is in sleep mode. I have not set the activity to turn screen on because I want to save as much battery as possible.

I've tested it with my phone and works really well, while screen is off all Handlers execute all the code they have to run. If I turn the screen on and off while the activity is open everything works fine.

Now, I've read everywhere that some devices does not work as expected when screen goes off and CPU sleeps, most of the times with accelerometers. Now my question is, do I need to acquire a WakeLock so the CPU does not sleep while my activity is open? I really want to know this because, as I said before, I don't want to 'waste' battery by acquiring an expensive WakeLock.

I would prefer a detailed answer of a person that really knows how this works.

Montevideo answered 26/6, 2013 at 15:42 Comment(2)
And why are you using Activities to do background tasks? An Activity is used for displaying things and not running tasks. Have you looked into Services?Cheryle
No no, the activity is not doing background tasks. Is an activity that opens with a Broadcast Receiver waiting for the user to check the phone and see it, but I don't want to add the flag KEEP_SCREEN_ON, that's why sometimes the activity is open with the screen off (or the screen turns off while the activity is open).Linin
H
27

Your goal cannot be stably achieved by your approach. If you use an Handler's postDelayed() method and the CPU goes to deepsleep, the ms counter will stop and will only continue if the CPU wakes up again.

See this for more detail.

So if you want to have some kind of a cron job you will need to use wakelock. Fortunately there is a Service implementation that does exactly that: Wakeful IntentService

From the doc:

The recommended pattern for Android's equivalent to cron jobs and Windows scheduled tasks is to use AlarmManager. This works well when coupled with an IntentService, as the service will do its work on a background thread and shut down when there is no more work to do. There's one small problem: IntentService does nothing to keep the device awake. If the alarm was a WAKEUP variant, the phone will only stay awake on its own while the BroadcastReceiver handling the alarm is in its onReceive() method. Otherwise, the phone may fall back asleep. WakefulIntentService attempts to combat this by combining the ease of IntentService with a partial WakeLock.

Handtohand answered 29/6, 2013 at 14:27 Comment(6)
Not bad, but it does not meet my requirements, it just keeps the CPU up with a WakeLock, like I do. The question is, I really need that partial WakeLock to keep my Handlers working while the screen is off and the activity is up? In my device works without WakeLock, but I don't know if that's the way all Android phones works or depends on device specification. If so, I want to know who told that, or where appears in documentation, because I can't find nothing. Thank you for your reply, that will come handy for some other things, CWAC has some cool libraries.Linin
As an altenative, have you tried AlarmManager.setInexactRepeating(), a power safing repeating alarm that sends an Intent when the alarm goes off? developer.android.com/reference/android/app/AlarmManager.htmlHandtohand
Some Handlers executes 2/3 times per second so I think that using AlarmManager for that is a bad idea. I have everything fine and working and does not consume much battery but if I can make the app consume less battery by removing WakeLock, then I'm going to do it. The only and main question is if I really need to use a WakeLock because in official documentation I've not read anywhere that I need that for handlers to work and in my phone the app works perfectly without WakeLock, but I don't know how is gonna work on another devices because the only Android device that I can use is mine :-/Linin
Maybe in the future if I earn a bit of money with apps I buy some devices for testing, but right now I can't :PLinin
I've looked into this further: you cannot gurantee that a handler.postdelayed will work as expected when the ui/activity is in background (see new link in edit answer). Its just not how android works, you have to design your app so that if you get an update of any kind and your app is in background you just have to handle it at the point your activity comes back to foreground and omit your background updates.Handtohand
At least!!!!! Also, after following some links inside the link you posted I've found this: groups.google.com/forum/?fromgroups#!topic/android-developers/… is an Android engineer confirming what that guy said... Just what I wanted! Really, thank you! So I need to wake up CPU with a WakeLock so does not enter in deep sleep mode, maybe my device is fine because is rooted and has Root Toolbox (I think that's the name of the app) tweaking CPU.Linin

© 2022 - 2024 — McMap. All rights reserved.