Chris
Chris

Reputation: 1311

android repeat alarm for every monday or every tuesday

I am developing an alarm based application, in which i have to repeat the alarm for every weekday like (every monday, tuesday, wednesday) based on user input. I used this snippet

Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, alarmToSetInMilliSeconds, sender);

If user selects every monday means, i found the milliseconds more to next monday date and i set an alarm, and its working fine, how can i repeat it for other next monday, I want some idea to achieve it, any help are appreciated. Thanks.

Upvotes: 1

Views: 2994

Answers (1)

chezi shem tov
chezi shem tov

Reputation: 438

here is my code to set alarm to repeat every month in 4th

public void init(Context context) {

    Intent intent = new Intent(context, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // Get the AlarmManager service
    alarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
}   
public void createAlarmControler(Context context) {

    init(context);
    Calendar cal = Calendar.getInstance();

    cal.set(Calendar.DAY_OF_MONTH, 4);
    cal.set(Calendar.HOUR_OF_DAY, 10);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    Date now = new Date(System.currentTimeMillis());
    if (cal.getTime().before(now)) {
        cal.add(Calendar.MONTH, 1);
    }
    long firstTime = cal.getTime().getTime();

    alarmManager.cancel(pendingIntent);
    Log.e("", "firstTime: " + firstTime);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, 1000 * 60
            * 60 * 24 * 30, pendingIntent);
}

public void cancelAlarmControler(Context context) {

    init(context);

    alarmManager.cancel(pendingIntent);
}

Upvotes: 1

Related Questions