CWAC Wakeful: Staying Awake At Work
The recommended pattern for Android's equivalent to cron
jobs and Windows scheduled tasks is to use
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
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
Any component that uses
startService() to send work to the
WakefulIntentService needs to call:
startService() to send over the work.
WakefulIntentService must override
doWakefulWork() instead of
will be processed within the bounds of a
the semantics of
doWakefulWork() are identical to
And that's it.
WakefulIntentService handles the rest.
NOTE: this only works with local services. You have no means of accessing the static WakeLock of a remote service.
This is version 0.1 of this module, meaning it is pretty darn new.
com.commonsware.cwac.wakeful.demo package you will find
OnBootReceiver designed to be attached to the
OnBootReceiver schedules an alarm, which is sent
OnAlarmReceiver in turn asks
extends WakefulIntentService) to do some work in a background
Note that when you build the JAR via
ant jar, the sample
activity is not included, nor any resources -- only the
compiled classes for the actual library are put into the JAR.
The code in this project is licensed under the Apache Software License 2.0, per the terms of the included LICENSE file.
If you have questions regarding the use of this code, please join and ask them on the cw-android Google Group. Be sure to indicate which CWAC module you have questions about.