set Repeating Alarm - Android android.app

Android examples for android.app:Alarm Set up

Description

set Repeating Alarm

Demo Code

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class Main {

  public static final String SET_ALARM = "com.kit.set_alarm";

  public static void setRepeatingAlarm(Context context, Intent intentBroadcast, int hour, int minute, int second,
      int millisecond, long interval) {
    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour);

    calendar.set(Calendar.MINUTE, minute);

    calendar.set(Calendar.SECOND, second);

    calendar.set(Calendar.MILLISECOND, millisecond);

    intentBroadcast.setAction(SET_ALARM);

    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intentBroadcast, PendingIntent.FLAG_UPDATE_CURRENT);

    alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, sender);

  }// w  ww.j  a  v a  2s .  c  o  m

  public static void setRepeatingAlarm(Context context, Intent intentBroadcast, long time, long interval) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentBroadcast,
        PendingIntent.FLAG_UPDATE_CURRENT);

    am.setRepeating(AlarmManager.RTC_WAKEUP, time, interval, pendingIntent);
  }

  public static void setRepeatingAlarm(Context context, PendingIntent pendingIntent, long time, long interval) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    am.setRepeating(AlarmManager.RTC_WAKEUP, time, interval, pendingIntent);
  }

}

Related Tutorials