Android UI How to - Setup Alarm Manager with PendingIntent








Question

We would like to know how to setup Alarm Manager with PendingIntent.

Answer

//  ww w  .j av  a2s  . c  om
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;

class AlarmManagerUtils {

  public static void setupAlarmManager(Context context, long minutesInMillis,
      PendingIntent pendingIntent) {
    AlarmManager alarmManager = (AlarmManager) context
        .getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
        + minutesInMillis, pendingIntent);
  }

  public static void cancelAlarmManager(Context context,
      PendingIntent pendingIntent) {
    AlarmManager alarmManager = (AlarmManager) context
        .getSystemService(Context.ALARM_SERVICE);
    pendingIntent.cancel();
    alarmManager.cancel(pendingIntent);
  }
}