4

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.

rajesh
  • 199
  • 1
  • 11

4 Answers4

3

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.

Community
  • 1
  • 1
Faraz
  • 2,144
  • 1
  • 18
  • 28
  • Thanks for your help. Adding interval to second param (i.e. triggerAtMillis) worked for me. – rajesh Apr 18 '17 at 09:28
  • Just to add ;) , AlarmManager.RTC , This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up. – dharmendra Sep 10 '17 at 17:44
0

Try this

alarmManager.setRepeating(
    AlarmManager.ELAPSED_REALTIME_WAKEUP,
    SystemClock.elapsedRealtime(),
    60000, 
    pendingIntent
);
Michael
  • 113
  • 3
  • 13
Rajesh N
  • 6,198
  • 2
  • 47
  • 58
0

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.

Strong
  • 99
  • 8
  • I am using MotoG3 having API level 23. I tried using both (i.e. setRepeating() and setInexactRepeating()) but I was facing same issue. Answer given by FAЯAƸ worked for me. Thanks for trying. – rajesh Apr 18 '17 at 09:33
0

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

lotfi Raghib
  • 434
  • 1
  • 6
  • 17