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?