Alarms Alarmmanager.setRepeating Immediately

Here is my code:

cal.set(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, hourOfDay, minute,Calendar.SECOND); Intent intent=new Intent(FaceActivity.this,AlarmReceiver.class); PendingIntent pendingIntent=PendingIntent.getBroadcast(FaceActivity.this, 0, intent, 0); AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 

Here hourOfDay is an hour before the current hour. a minute may have the same meaning.

Now here cal.getTimeInMillis () is set for a time that is one hour longer than the current time. But when I run this code, the broadcast receiver is called Immediately. Can anyone tell me what I'm doing wrong? Thanks at Advance ...

+4
source share
1 answer

Finally, I found my error, I got the wrong values ​​for Year , Month , Day , Second .

Using the code below, it works great.

 cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), hourOfDay, minute,cal.get(Calendar.SECOND)); 
+2
source

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


All Articles