Reputation: 21
I am developing an app for internal needs, that has to run some code up to 100 times every ~ 5 minutes. So I am using AlarmManager. I declare permissions in the Manifest:
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM"/>
And here is the function with which I set the alarm. Based on some logic RequestsReceiver will either schedule or not the next alarm for the request id:
fun scheduleRequest(requestId: Long, timestamp: Long) {
val intent = Intent(app, RequestsReceiver::class.java).apply {
putExtra(EXTRA_REQUEST_ID, requestId)
}
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
timestamp,
PendingIntent.getBroadcast(
app,
requestId.hashCode(),
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
)
}
On my Samsung Galaxy s8 (api 28) it worked as expected about 9 hours, after which it started to set alarms every 10 minutes instead of 6, then it became 1.5 hours and so on. On Xiaomi Android 14 seems it postpones next alarm for 1 hour, and then it can run some of them as intended. I only find information about alarms being non-exact, and not intentionally postponed, is there any explanation and a work-around?
Upvotes: 0
Views: 25
Reputation: 303
As you can see, if your app targets Android 12 or higher, the Android documentation mentions that it should be at least 10 minutes. For workaround also there are some methods and documentation present in there. Can give it a try!!!
Upvotes: 0