我有以下代码可在android中设置重复警报
private static void setAlarm (Context context, int hour, int minute) {
//Log.i("AMIRA000" , "alarm schedule in : " + hour + " : " + minute);
Calendar alarmCalendarTime = Calendar.getInstance();
alarmCalendarTime.set(Calendar.HOUR_OF_DAY, hour);
alarmCalendarTime.set(Calendar.MINUTE, minute);
alarmCalendarTime.set(Calendar.SECOND, 0);
alarmCalendarTime.setTimeZone(TimeZone.getTimeZone("gmt"));
String diff = alarmCalendarTime.getTime().toString().substring(alarmCalendarTime.getTime().toString().indexOf("GMT"),
alarmCalendarTime.getTime().toString().lastIndexOf(":"));
diff = diff.replace("GMT" , "");
Log.i("AMIRA000" , "diff str " + diff);
int diffHour = 0;
if (diff.contains("+")) {
diffHour = Integer.parseInt(diff.replace("+" , ""));
} else {
diffHour = -1 * Integer.parseInt(diff.replace("-" , ""));
}
if (alarmCalendarTime.before(Calendar.getInstance())) {
alarmCalendarTime.add(Calendar.DAY_OF_MONTH, 1);
Log.i("AMIRA000" , "tomorrow schedule : " + alarmCalendarTime.getTime() + " : " + (alarmCalendarTime.getTimeInMillis() + TimeUnit.HOURS.toMillis(diffHour)));
} else {
Log.i("AMIRA000" , "today schedule : " + alarmCalendarTime.getTime() + " : " + (alarmCalendarTime.getTimeInMillis() + TimeUnit.HOURS.toMillis(diffHour)));
}
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent receiverIntent = new Intent(context, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, receiverIntent, 0);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, (alarmCalendarTime.getTimeInMillis() + + TimeUnit.HOURS.toMillis(diffHour)), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS), alarmIntent);
}
代码需要花费小时和分钟,并尝试根据国家/地区和时间表警报来计算时间
我打印了警报的值,这似乎是正确的 但是今天警报不起作用,我必须提前一天触发onReceive方法
谁能告诉我出了什么问题