所以基本上我有这个代码,时间返回 24 小时时间并每天重复警报。
public setAlarm(String time, Context context){
String[] strTime;
strTime = time.split(":");
int hour, min, sec;
//set when to alarm
hour = Integer.valueOf(strTime[0]);
min = Integer.valueOf(strTime[1]);
sec = 0;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, sec);
//Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(context, AlarmReceiverActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 19248, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
无论如何,问题是当我在上午 9 点设置闹钟时,时间是上午 9 点 10 分,闹钟会响起。为什么?如果设置超过系统时间,我希望它不会发出警报。前任。将闹钟设置为上午 9 点,而系统时间为上午 9 点 10 分