0

I need to call alarm Manger for the interval of every 2 minutes,I have implemented below code :-

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, downloader, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + (2 * 1000*60),(2 * 1000*60), pendingIntent);

I am facing problem that it is not working on exact interval of 2 minutes,means it is getting fired sometimes before one minute some time after 2 minutes

but i need to generate logs of this with the exact interval of 2 minutes,wheather phone is on wake up mode or in sleep mode.

I am using api level 23 for this functionality. please help....

Santosh Yadav
  • 338
  • 2
  • 4
  • 15
  • Please consult the docs for [`AlarmManager`](http://developer.android.com/reference/android/app/AlarmManager.html). As of KitKat, if your `targetSdkVersion`>=19, `setRepeating()` is inexact. If you want exact times, you'll need to use a `setExact*()` method, and set the alarm again for the desired interval each time it fires. – Mike M. Nov 19 '16 at 05:05
  • hi Mike Thanks for response but still after using setExact*() method I am not getting accuracy of two minutes **alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (2* 1000*60), pendingIntent);** – Santosh Yadav Nov 19 '16 at 10:49
  • I'm not sure exactly what you mean, but I would mention that the first trigger time for an alarm is usually off by a little bit, in my experience anyway. If you mean it's not exact when idle, you might have a look at [this post](http://stackoverflow.com/q/33110246). Beyond that, newer Android versions are progressively placing more and more restrictions on `AlarmManager`, 'cause developers keep misusing it, and killing users' batteries. If you absolutely must have exact, 2-minute intervals, `AlarmManager` might not be the right solution for you. – Mike M. Nov 19 '16 at 11:10
  • Then instead of AlarmManger what should i use for this scenario. – Santosh Yadav Nov 19 '16 at 11:17
  • CommonsWare outlines possible solutions in [his answer](http://stackoverflow.com/a/33110418) on that linked post. – Mike M. Nov 19 '16 at 11:21
  • Possible duplicate of [setExactAndAllowWhileIdle - is not exact as of developer reference](https://stackoverflow.com/questions/33110246/setexactandallowwhileidle-is-not-exact-as-of-developer-reference) – Vadim Kotov Oct 31 '19 at 11:51

0 Answers0