As per Google documentation. https://developer.android.com/reference/android/app/AlarmManager.html “the Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.” That is exactly my use case. I create Alarm with PendingIntent in my app setting few mins in the future, then I close the application. OS needs to deliver Intent via Broadcast and wake up my app. Application then triggers some internal function once Intent delivered to the app. All works fine if my application is running all the time:
Relevant code:
//Alarm creation
val alarmManager =
myApp.applicationContext().getSystemService(Context.ALARM_SERVICE) as AlarmManager
if (alarmManager.canScheduleExactAlarms()) {
try {
val alarmIntent = PendingIntent.getBroadcast(
myApp.applicationContext(),
id,
intent,
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
triggerAtMillis, // few mins in the future
alarmIntent
)
} catch (e: SecurityException) {
// Print error
}
// handling Intent:
class AlarmReceiver : BroadcastReceiver() {
var context: Context = myApp.applicationContext()
override fun onReceive(context: Context, intent: Intent) {
// do stuff here
}
Application has
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
Im running Android 12 , device is a TV box which powered all the time (no buttery) , holds WakeLock.
Any ideas why Android is not launching my application and delivering PendingIntent. I feel Im doing something wrong but cant figure out what…
Thanks
David