Chirag Patel
Chirag Patel

Reputation: 1611

how to set alarm on monday using alarm manager in android?

I am trying to set alarm on Monday but its not working proper so how to set alarm on Monday only once time.

if (chk_wednesday.isChecked()) {
                        weekday = 7 - calNow.get(Calendar.DAY_OF_WEEK);
                        calSet.add(Calendar.DATE, weekday);
                        forday(weekday);
                        }


public void forday(int week) {

                days = calNow.get(Calendar.DAY_OF_MONTH);
                month = calNow.get(Calendar.MONTH);
                year = calNow.get(Calendar.YEAR);

        calSet.set(Calendar.DAY_OF_WEEK, week);
        calSet.set(Calendar.HOUR_OF_DAY, hour);
        calSet.set(Calendar.MINUTE, minuts);
        calSet.set(Calendar.SECOND, 0);
        calSet.set(Calendar.MILLISECOND, 0);

        calSet.set(Calendar.DAY_OF_MONTH, days);
        calSet.set(Calendar.MONTH, month);
        calSet.set(Calendar.YEAR, year);

        if (calSet.compareTo(calNow) <= 0) {
            // Today Set time passed, count to tomorrow
            calSet.add(Calendar.DATE, 1);
        }
        Log.i("time1", "" + calSet.getTimeInMillis());
        alarmManager.set(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),
                pendingIntent);
    }

Any one help me.Thanks in Advance

Upvotes: 0

Views: 1076

Answers (1)

UgglyNoodle
UgglyNoodle

Reputation: 3047

I would do it like this, where day_of_week, hour, minute define the day and time you want the alarm to occur. You set calSet to the appropriate time for this week. Then, if that time has already passed this week, you add a week so it will be set to the appropriate time for next week.

calNow = Calendar.getInstance();

calSet = Calendar.getInstance();
calSet.set(Calendar.DAY_OF_WEEK, day_of_week);
calSet.set(Calendar.HOUR_OF_DAY, hour);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);

if (calSet.before(calNow)) {
    nextAlarm.add(Calendar.WEEK_OF_YEAR, 1);
}

Upvotes: 1

Related Questions