AlarmManager
The AlarmManager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.
The following little demo is developed to learn about AlarmManager:
1. Manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.slowalker.alarmdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.slowalker.alarmdemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.slowalker.alarmdemo.PollingService" android:label="@string/app_name" > <intent-filter> <action android:name="com.slowalker.alarmdemo.pollingservice.action"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </service> </application> </manifest>
2. MainActivity.java
package com.slowalker.alarmdemo; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; public class MainActivity extends Activity { private static final String TAG = MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (Switcher.DEBUG) { Log.d(TAG, "onCreate()"); } setContentView(R.layout.activity_main); PollingUtils.startPollingService(this, 3, PollingService.class, PollingService.ACTION); } @Override protected void onDestroy() { super.onDestroy(); if (Switcher.DEBUG) { Log.d(TAG, "onDestroy()"); } PollingUtils.stopPollingService(this, PollingService.class, PollingService.ACTION); } }
3. PollingUtils.java
package com.slowalker.alarmdemo; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.SystemClock; public class PollingUtils { /** * Start polling service. * @param context context * @param seconds polling cycle * @param cls classname * @param action action of intent */ public static void startPollingService(Context context, int seconds, Class<?> cls, String action) { /*Get system service of AlarmManager.*/ AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); /*Set the intent of the polling.*/ Intent intent = new Intent(context, cls); intent.setAction(action); PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); /*Get the begin time of tiggering polling.*/ long triggerAtTime = SystemClock.elapsedRealtime(); /*Invoke method setRepeating() to implement polling per seconds(in second) time.*/ manager.setRepeating(AlarmManager.ELAPSED_REALTIME, triggerAtTime, seconds * 1000, pendingIntent); } /** * Stop polling service. * @param context context * @param cls classname * @param action action of intent */ public static void stopPollingService(Context context, Class<?> cls, String action) { /*Get system service of AlarmManager.*/ AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); /*Set the intent of the polling.*/ Intent intent = new Intent(context, cls); intent.setAction(action); PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); /*Invoke method cancel() to cancel the polling.*/ manager.cancel(pendingIntent); } }
4. PollingService.java
package com.slowalker.alarmdemo; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class PollingService extends Service { private static final String TAG = PollingService.class.getSimpleName(); public static final String ACTION = "com.slowalker.alarmdemo.pollingservice.action"; @Override public IBinder onBind(Intent intent) { if (Switcher.DEBUG) { Log.d(TAG, "onBind()"); } return null; } @Override public void onCreate() { if (Switcher.DEBUG) { Log.d(TAG, "onCreate()"); } } @Override public void onStart(Intent intent, int startId) { if (Switcher.DEBUG) { Log.d(TAG, "onStart()"); } new Thread(new Runnable() { @Override public void run() { if (Switcher.DEBUG) { Log.d(TAG, "Polling..."); } System.out.println("polling service works..."); } }).start(); } @Override public void onDestroy() { if (Switcher.DEBUG) { Log.d(TAG, "onDestroy()"); } } }