I am setting an alarm with alarm manager to run at 6PM. If it is already 6PM, I want it to run the next day at 6PM.
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
if(calendar.get(Calendar.HOUR_OF_DAY) < 18) {
calendar.set(Calendar.HOUR, 18);
calendar.set(Calendar.MINUTE, 0);
} else {
// Set sync time
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR, 18);
calendar.set(Calendar.MINUTE, 0);
}
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
alarmIntent.putExtra("syncmode", "upload");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
I have 6 Android tablets all running the same exact code and some are waking up at the correct times, others are running hours later. I have checked the timezones and everything matches. The documentation for AlarmManager says to use UTC time to set the alarm, but in practice I am using local time and the ones that are working are firing at the correct time. The others are firing 4 hours before time. What am I doing wrong?
Edit: I cannot answer my own question so, I was setting Calendar.HOUR instead of HOUR_OF_DAY which was causing the time to be set incorrectly.
Just so this will be removed from the "Unanswered" list:
I was setting Calendar.HOUR instead of HOUR_OF_DAY which was causing the time to be set incorrectly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.