Android Programming Tutorials

(Romina) #1
Now, Your Friends Are Alarmed

package="apt.tutorial.two"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".Patchy"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".EditPreferences">
</activity>
<activity android:name=".StatusMap">
</activity>
<activity android:name=".HelpCast">
</activity>
<activity android:name=".HelpPage">
</activity>
<service android:name=".PostMonitor" />
<receiver android:name=".OnAlarmReceiver">
</receiver>
</application>
</manifest>

OnAlarmReceiver tells WakefulIntentService to sendWakefulWork(), which will:



  • Acquire a static WakeLock, then

  • Call startService() on our PostMonitor service


This means all WakeLock management can be encapsulated in


WakefulIntentService.


Step #3: Doing the Work


Now, we need to arrange for PostMonitor to respond to those polling


Intents.


First, change PostMonitor to have it extend WakefulIntentService, adding an


import for com.commonsware.cwac.wakeful.WakefulIntentService.


291
Free download pdf