1

I need to repeat my Alarm manager every minute. So i wrote the code like

  val calendar = Calendar.getInstance()
    calendar.timeInMillis = System.currentTimeMillis()
    calendar.add(Calendar.MINUTE, 0)
    calendar.set(Calendar.SECOND, 0)

    val triggerAt = System.currentTimeMillis()
    val oneMinute = (60 * 1000).toLong()

    val alarmMgr = getSystemService(Context.ALARM_SERVICE) as AlarmManager
    val intent = Intent(this, BootCompleteReceiver::class.java)
    val alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0)
    alarmMgr.cancel(alarmIntent)


    alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + 1000 * 60,
            1000 * 60, alarmIntent)

But sometimes it's repeating within a minute.

Please help me!

PPV
  • 107
  • 1
  • 1
  • 8
  • If you are experiencing accuracy issues, then it is expected. You will experience much worse accuracy if the device enters doze mode. Take a look at this: https://stackoverflow.com/questions/45079300/android-alarm-manager-setexactandallowwhileidle-not-working-in-android-7-0-nou – Mena May 14 '19 at 10:29
  • @Mena did you find any solution? – PPV May 14 '19 at 10:35
  • No, I don't think there is one. Maybe if u gain root access to the device, you might override some android restrictions. But even then, in your case, i doubt it would get you an accurate alarm to the second, so you would have to expect alarms a couple of seconds earlier or later. – Mena May 14 '19 at 10:39
  • @Mena seconds doesn't matter for me. But the class is calling two to three times within a minute. Not every time, only some times i am facing the issue. – PPV May 14 '19 at 10:43
  • Does this happen while the the app is in the foreground? Which Android version does your device have? – Mena May 14 '19 at 11:49
  • @Mena yes. Oreo – PPV May 15 '19 at 04:58

0 Answers0