rajesh
rajesh

Reputation: 199

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.

Upvotes: 4

Views: 5794

Answers (5)

Ransford Owusu
Ransford Owusu

Reputation: 114

NB: I use kotlin, but it wont make much difference
I recently faced an issue like this.Based o. some reason i don't know yet, alarm manager uses Unix epoch time, i.e, January 1, 1970.

You can fix this by just taking your delay(maybe 2 minutes in milliseconds), then add System.currentTimeMillis or Calender.getInstance().timeInMillis.

So it will look something like this in the end, assuming our delay should be 11 seconds.

val delay = 11000 + System.currentTimeMillis()
alarmManager.setExact(AlarmManager.RTC_WAKEUP, delay, intent)

Upvotes: 0

Rajesh N
Rajesh N

Reputation: 6693

Try this

alarmManager.setRepeating(
    AlarmManager.ELAPSED_REALTIME_WAKEUP,
    SystemClock.elapsedRealtime(),
    60000, 
    pendingIntent
);

Upvotes: 0

lotfi Raghib
lotfi Raghib

Reputation: 514

i had the same issue, and i solve it by removing the

android:exported="true"

from my receiver in the Manifest, because this attribut make your receiver to receive messages from sources outside its application, check this link android receiver

Upvotes: 0

Faraz
Faraz

Reputation: 2154

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.

Upvotes: 3

Strong
Strong

Reputation: 99

In my device (Nexus5 cm13), it can work well using below code :

    private void doWork() {
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, SecondActivity.class), 0);
        final AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.cancel(pendingIntent);
        long interval = 60000;
        alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
            interval, pendingIntent);
   }

So I don't know it clearly, and you can try "setRepeating" for testing.

Upvotes: 0

Related Questions