0

I have set up a notitication using setRepeating with AlarmManager and when testing I noticed that the repeating process waits 5 minutes to send a notification when it should fire it every minute. I believe its me who isnt doing something correctly. Could anybody please look ay my code below and point me in the right direction? Thank you.

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY,calendar.get(Calendar.HOUR_OF_DAY) );
calendar.add(Calendar.MINUTE, calendar.get(Calendar.MINUTE));

 SimpleDateFormat sdf = new 
 SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
 String date = sdf.format(calendar.getTime()); 
 Date date1 = calendar.getTime();
 long differenceInMillis = date1.getTime();

 System.out.println("dates are " +date1);
Toast.makeText(getApplicationContext(), "Alarma Set", Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this, BroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,intent,0);
AlarmManager alarmManager =(AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeAtButtonClick/1000, 1000 * 60 * 1, pendingIntent);
OhhhThatVarun
  • 3,981
  • 2
  • 26
  • 49
  • `setRepeating` alarm are In exact .. if you need exact alarm I think you should use `setExact()` for one shot alarm and then set next alarm for future one it processed . have a Look at [This Thread](https://stackoverflow.com/a/47651310/4168607) .. – ADM Apr 14 '20 at 04:54
  • Thanks for your reply! So, I must basically use a one time Alarm and then schedule the next alarm. How would I go about doing that? Can you provide an example code? I understand what I would need to do but the documentation provides a suggestion only. – LCDTM Films Apr 14 '20 at 05:24
  • Do you want it to alram every 1 minute? – Ticherhaz FreePalestine Apr 14 '20 at 06:28
  • Ticherhaz. Not precicely. See, the time interval varies. I get the time based in a calculation from user input. Kind of like a reminder. I am converting the output from decimal to int so when I multiply 1000*60* X the X will represent the minutes the alarm should repeat. – LCDTM Films Apr 15 '20 at 00:08

0 Answers0