The CommonsBlog

WakefulIntentService Updated, Welcomes Its New Donut Overlord

Complaints occasionally rolled in for WakefulIntentService, stemming from random WakeLock issues being reported in the field.

For those of you who have no idea what I am talking about, WakefulIntentService is an IntentService implementation that manages a WakeLock. Used properly, it ensures that your periodic AlarmManager work will run to completion without the device falling back asleep, if you are using a _WAKEUP-style alarm. This is key for many polling services.

Near as I am able to tell, the problem stemmed from my use of onStart() in the service. If the service were to crash, Android would restart it, but without re-delivering an Intent. As a result, the WakeLock would never be acquired, and so it would generate an under-locked exception when the work was eventually completed.

To combat this, I am discontinuing support for Android 1.6 and older. v0.4.0 of the service now requires API level 5 or higher, and uses onStartCommand(). I am trying START_REDELIVER_INTENT semantics now, as that should be enough to prevent these errors. If that proves to be insufficient, I may fall back to START_NOT_STICKY, basically abandoning the current batch of work and waiting for the next alarm or whatever.

As always, if you have questions or concerns about this component, please join the cw-android Google Group and post there!

Do you need a book on Android app development, but are having trouble finding one that is new enough? Try The Busy Coder’s Guide to Android Development!