Android AlarmManager shoots at the wrong times

I set an alarm with an alarm manager to start from 6pm. If it's already 6pm, I want him to start 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 with the same code, and some wake up at the right time, others work after a few hours. I checked the time and all matches. The documentation for AlarmManager says to use UTC time to set an alarm, but in practice I use local time, and those that work shoot at the right time. The rest shoot 4 hours before time. What am I doing wrong?

Edit: I cannot answer my question, so I installed Calendar.HOUR instead of HOUR_OF_DAY, which caused the wrong time.

+4
source share
1 answer

Just so that this is removed from the Unanswered list:

I installed Calendar.HOUR instead of HOUR_OF_DAY, which caused the time to be set incorrectly.

+2
source

Source: https://habr.com/ru/post/1385602/


All Articles