我正在使用日历。我使用下面的代码来设置闹钟。但!!!如果我尝试在星期一设置闹钟(Calendar.Monday),则实际闹钟将在星期二设置。为什么会这样?我也尝试使用Calendar.getInstance(Locale.getDefault())
,但它没有用。
这就是我为周日设置闹钟的方式:
setAlarm(Calendar.SUNDAY, h, m, k, y);
我的setAlarm()方法:
public void setAlarm(int dayOfWeek, int hour, int minute, int position, int y) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context,MyReceiver_Alarm.class);
Long alarmTime = calendar.getTimeInMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), position , intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
Log.e("Point_1", "Time is " + calendar.getTime());
}
谢谢。
答案 0 :(得分:2)
DayOfWeek 0 ,从星期日开始。 因此,如果你传递2,认为它是基于1的,那么你就是在周二。
答案 1 :(得分:1)
The parameter Calendar.DAY_OF_WEEK starts from SUNDAY, so if u want to set alarm for monday set the value of int dayOfWeek to 2 . refer image link