Android Programming Tutorials

(Romina) #1

TUTORIAL 30


Now, Your Friends Are Alarmed


One current flaw with Patchy is that if your device goes to sleep, you will


not get status updates on your timeline. Clearly, this must be corrected.


In this tutorial you will have PostMonitor use AlarmManager, instead of its


own polling loop, to wake it up as needed. You will also use a WakeLock to


ensure that PostMonitor stays awake long enough to do a poll of the


timeline. Fortunately, much of this is wrapped up in a reusable component


that you will employ.


Step-By-Step Instructions........................................................................


First, you need to have completed the previous tutorial. If you are


beginning the tutorials here, or if you wish to not use your existing work,


you can download a ZIP file with all of the tutorial results, and you can copy


the 29-SysEvents edition of Patchy to use as a starting point.


Step #1: Import a Reusable Component...........................................


Some guy wrote WakefulIntentService, which is covered in some detail in


The Busy Coder's Guide to Advanced Android Development.


WakefulIntentService is an ideal component to use in this application, to


ensure that if the device wakes up in the middle of the night to poll Twitter


for updates, that the device does not fall back asleep in the middle of doing


that poll.


289
Free download pdf