Wake Service directly from AlarmManager
Asked Answered
A

1

8

In the https://github.com/commonsguy/cwac-wakeful demo, the OnAlarmReceiver (a BroadcastReceiver) onReceive() method is called in response to an Alarm. The onReceive() method starts the Service. There are two Intents used, one received by the BroadcastReceiver and one by the Service. This seems more complicated that it needs to be, why not just have the Service receive the Intent from the AlamManager?

I understand that the Phone is guaranteed not to sleep while onReceive() is executing i.e. it wrapped with a wake lock. I'm not sure if the Service class offers any similar guarantees.

Is the any way to start the Service directly from an Alarm while still guaranteeing that the Phone will be woken from sleep and won't sleep until a wake lock can be acquired?

Answerable answered 16/9, 2011 at 11:40 Comment(0)
C
14

This seems more complicated that it needs to be, why not just have the Service receive the Intent from the AlamManager?

Because Android will not guarantee that the device will stay awake long enough for a getService() PendingIntent to be invoked. The guarantee is only for a getBroadcast() PendingIntent -- Android ensures that the device will stay awake (via a WakeLock) for the duration of your onReceive() call.

Trust me, I wish we could skip it.

Czarevna answered 16/9, 2011 at 12:15 Comment(5)
oh that's great - I was wondering about the same thing - apparently in the simple alarm example from the API samples this is not a concern - or am I missing something ?Eliaseliason
@Mr_and_Mrs_D: That sample has so many issues, it is not even funny. Someday, I'll file an issue to hopefully get it changed.Czarevna
Please do (mention the comments also) - in my short acquaintance with Android I have already seen more clumsy, buggy, inelegant, confusing code as ever before. What a mess !Eliaseliason
Where is this documented? Could you @Czarevna point me to the source?Wentworth
@VitoValov: "Where is this documented?" -- it's not, outside of some old android-developers Google Group posts from Dianne Hackborn and related materials.Czarevna

© 2022 - 2024 — McMap. All rights reserved.