Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Alarm manager is repeating after 5 seconds and ignoring interval time

I am working on a widget application where I have to perform some task in every one minute. So, I am using AlarmManager to achieve this. But no matter whatever I set the interval time, its being repeated in every 5 seconds.

I am using AlarmManager like this:

 final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarm.cancel(pendingIntent);
        long interval = 60000;
        alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), interval, pendingIntent);

Thanks in advance.

like image 223
rajesh Avatar asked Oct 24 '25 03:10

rajesh


1 Answers

AlarmManager.ELAPSED_REALTIME is used to trigger the alarm since system boot time. Whereas AlarmManager.RTC uses UTC time.

 alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), interval, pendingIntent);

This will start running after system boots and repeats at the specified interval.

alarm.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), interval, pendingIntent);

This will start running from now and repeats at the specified interval.

To solve the problem, I suggest using AlarmManager.RTC. In case you want to start the alarm after 1 minute and then repeat, then pass the second param like this:

calendar.getTimeInMillis() + interval

Also check out the android documentation and this answer for more explanation in Alarms.

like image 87
Faraz Avatar answered Oct 26 '25 14:10

Faraz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!