Johann
Johann

Reputation: 29877

Create a Wi-Fi wakelock for an AlarmManager alarm

I use AlarmManager to start a periodically repeating alarm. I need the Wi-Fi to always remain connected, so I believe that a wakelock on the Wi-Fi is necessary. What isn't clear to me is where I create this wakelock and avoid creating it each time the alarm is triggered. There are no activities in my app.

I know that I can assign a tag to a wakelock but there doesn't seem to be any way to interrogate Android to see if a wakelock exists with the tag. If I could do that, I could tell that the wakelock already exists when the alarm triggers and not bother recreating it. Or is there an alternative solution?

Upvotes: 1

Views: 472

Answers (2)

Edu Barbas
Edu Barbas

Reputation: 467

I had to face a somewhat similar scenario. I also user AlarmManager to trigger a service periodically, where the WiFi has to be on at all times, even if the phone's screen is locked. I found that changing the WiFi sleep policy, either manually or programmatically, was enough for many devices, and did not need to use a wakelock. However, on some HTC devices, I found this setting not working at all at night. I monitored my app and found out that the WiFi would turn off between 00:00 (midnight) and 7:00 in the morning - working like a charm the rest of the time. I also found a post elsewhere (not stackoverflow) where the author mentioned something about a special setting for HTC phones being able to cope with this issue:

Settings.System.putInt(getContentResolver(), "smartsync_turn_off_wifi", 0);

This happened about one and a half year ago, and using this setting saved me on any HTC device up to Android 3.1, at least until recently. Not so long ago, some users of our app are reporting that their HTCs, with Android 4.x, are not doing what they are supposed to do because the WiFi turns off at night, so it seems the mysterious setting I used does not work anymore on newer devices.

Anyways, @AndroidDev, since your issue and my issue appear to be somewhat related, I would like to know if you have managed to solve it, and if the response is affirmative, how.

Upvotes: 1

Tom
Tom

Reputation: 17882

I think there is a way to get a list of the existing wakelocks (with tags), but I question whether that is the right solution. Check out an app like 'wakelock detector' - I believe it retrieves the list and matches the tags with a known list of tags in order to figure out which app created it. Also note the duration of the wake locks - a CPU wakelock is intended for very short term use. I don't know what the period of your alarms are, but it sounds like you might be intending to hold the wakelock for too long.

When you refer to "a wakelock on the Wi-Fi", I'll assume you just mean a regular wakelock - there are wakelocks for the screen and for the CPU, but no such thing as a wakelock for the wifi. Also, finding your own wakelock by tag doesn't make sense to me - if you don't still have a reference to your wakelock then how can you release it?

You write that you "need the Wi-Fi to always remain connected". If the wifi AP remains available then the wi-fi will remain connected - even if the device goes to sleep - as long as the user hasn't changed the 'keep wifi on during sleep' setting. Your app can check this setting.

Upvotes: 0

Related Questions