如何使用android中的报警管理器在星期一设置闹钟?

时间:2012-09-27 05:33:49

标签: android alarmmanager alarm

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

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

任何人帮助我。谢谢你提前

1 个答案:

答案 0 :(得分:1)

我会这样做,其中day_of_weekhourminute定义您希望闹钟发生的日期和时间。您将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);
}