0

无论何时(如果过去),我的重复闹钟都会在周日第一次开始,这是问题所在,在下周日闹钟正确重复,我想从下周日开始闹钟..这是我的代码:

 AlarmManager alarmMgr = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);

            Intent intent = new Intent(activity, Alarm.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);


            Calendar calendarStart = Calendar.getInstance();

            calendarStart.set(Calendar.DAY_OF_WEEK, 1 );
            calendarStart.set(Calendar.HOUR_OF_DAY,8);
            calendarStart.set(Calendar.YEAR,calendarStart.get(Calendar.YEAR));              
            calendarStart.set(Calendar.MINUTE, 0);
            calendarStart.set(Calendar.SECOND, 0);

            Toast.makeText(activity, calendarStart.getTime()+"",Toast.LENGTH_LONG).show();



            alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendarStart.getTimeInMillis() ,24*60*60*1000, pendingIntent);
4

1 回答 1

1

无论何时(如果过去),我的重复闹钟都会在星期日第一次开始

检查是否calendarStart是过去以及是否添加 7 天:

if(calendarStart.compareTo(Calendar.getInstance()) < 0) { 
    // calendarStart is in the past, use next Sunday
    calendarStart.add(Calendar.DAY_OF_YEAR, 7);
}

Toast.makeText(activity, calendarStart.getTime()+"",Toast.LENGTH_LONG).show();
于 2013-02-12T20:22:50.580 回答