0

I have some weird issue with AlarmManager and repeating alarm.

I'm setting repeating alarm to run every one minute:

val i = Intent(applicationContext, CheckStatusReceiver::class.java)
val alarmIntent = PendingIntent.getBroadcast(context.applicationContext, 1001, i, PendingIntent.FLAG_UPDATE_CURRENT)
val alarmManager = applicationContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 60 * 1000, 60 * 1000, alarmIntent)

The problem is that when device has screen off and is not connected to computer (adb), my BroadcastReceiver is not always called. Sometimes it is called every minute but sometimes there are few minutes without any call! I tested it on few devices and it happens almost every time on 4.2, 4.4 and sometimes on 6.0.1, just few times happend on 5.1 and 7.1. It doesn't matter if battery is full or empty.

I have my received registered in AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test.example.package">
    [... permissions ...]
    <application
        [....]
        <receiver android:name=".receiver.CheckStatusReceiver" />
    </application>
</manifest>

And my receiver is just logging for test purposes:

class CheckStatusReceiver: BroadcastReceiver(), AnkoLogger {
    override fun onReceive(context: Context, intent: Intent?) {
        info("CheckStatusReceiver onReceive called")
    }
}

When screen is on or device is connected to adb with screen off alarms usually (99%) works like a charm.

What should I do to assure that alarm is always called?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user3626048
  • 706
  • 4
  • 19
  • 52
  • 1
    I would check this question: https://stackoverflow.com/questions/32492770/how-to-make-alarm-manager-work-when-android-6-0-in-doze-mode – HedeH Jul 01 '18 at 12:45
  • @HedShafran thank you, I will try that. I hope it helps but problem is also on Android 4.2 and the answer in post says that versions below KitKat should work without any modifications – user3626048 Jul 01 '18 at 13:37
  • 1
    You're welcome.. Android has brought a lot of changes and restrictions since Oreo.. We need to adjust accordingly.. I would also read this, maybe it's even better than the first link :) ==> https://medium.com/@benexus/background-services-in-android-o-862121d96c95 – HedeH Jul 01 '18 at 13:40
  • @HedShafran problem is that I'm setting up repeating alarm and there are no such method to set up repeating alarm ...AndAllowWhileIdle. How should I proceed? Set up one-time alarm and set up another one in first alarm execution? – user3626048 Jul 01 '18 at 21:33
  • 1
    Exactly.. Set another one-time alarm from the callback of the first one.. This will create the behaviour of a repeating task. – HedeH Jul 02 '18 at 06:16

0 Answers0