Home » Android » Creating a repeating alarm [duplicate]

Creating a repeating alarm [duplicate]

Posted by: admin November 1, 2017 Leave a comment

Questions:

This question already has an answer here:

Answers:

Hello this is an example of adding an alarm once a month.

 private void setAlarmForOneMonth() {

    AlarmManager mAlarmMgr = (AlarmManager) CONTEXT.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(CONTEXT, YOURALARMRECEIVERCLASS);

    PendingIntent mAlarmIntent = PendingIntent.getBroadcast(CONTEXT, ONE_MONTH_ALARM, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    mAlarmMgr.set(AlarmManager.RTC_WAKEUP, getDuration(), mAlarmIntent);
}

private long getDuration() {

    Calendar calendar = Calendar.getInstance();
    int updatedMonthValue = calendar.get(Calendar.MONTH) + 1;
    calendar.set(Calendar.MONTH, updatedMonthValue);

    return calendar.getTimeInMillis(); // this is what you set as trigger point time i.e one month after

}

Also you can read more on this link Alarm Manager