0

我想在星期一设置闹钟,但它不能正常工作,所以如何在星期一只设置一次闹钟。

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);
    }

任何人帮助我。在此先感谢

4

1 回答 1

1

我会这样做,其中day_of_week, hour,minute定义您希望警报发生的日期和时间。您将 calSet 设置为本周的适当时间。然后,如果该时间本周已经过去,则添加一周,以便将其设置为下周的适当时间。

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);
}
于 2012-09-27T06:13:27.223 回答