I'm trying to send a broadcast intent using alarm manager roughly every 60 seconds
I'm using the following function to set the alarm manager:
fun alarm(context: Context) {
println("load alarms5")
val alarmManager =
context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val alarmIntent = Intent(context, CheckAlarms::class.java).let { intent ->
PendingIntent.getBroadcast(context, -1, intent, PendingIntent.FLAG_IMMUTABLE)
}
val calendar: Calendar = Calendar.getInstance().apply {
timeInMillis = System.currentTimeMillis()
}
alarmManager.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
calendar.timeInMillis,
1000*60,
alarmIntent
)
println("-alarm search-")
}
If I use the function normally it sets the alarm correctly and I every 60 seconds or so the intent is sent.
But when I use the following function to set the alarm when the phone boots, the alarm manager only runs once without repeating, and the intent is only sent once.
class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == "android.intent.action.BOOT_COMPLETED") {
PlannerAlarmManager.alarm(context)
}
}
Manifext:
<receiver android:name="pt.keeponcare.planner.BootReceiver"
android:enabled="false"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
This code runs when the app is opened:
context.packageManager.setComponentEnabledSetting(
receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP
)