I am trying to set an android alarm manager, alarmMgr?.setAlarmClock . I have the code towards the bottom in my recyclerview adapter. It works fine to set the alarm. But in my dialog fragment, the code toward the top, whenever I hit the button to close the dialog fragment and also set the alarm, it does not work. I do not get an error or anything. Is this a context issue where I am using the wrong context? Or anyone have any ideas?
//the below in my dialog fragment will not set an internal alarm
var alarmIntent: Intent
var alarmPendingIntent: PendingIntent
alarmIntent = Intent(rootView.context, AlarmReceiver::class.java)
alarmIntent.putExtra("alarmTimeEntityId", 0)
alarmPendingIntent = PendingIntent.getActivity(rootView.context, 0, alarmIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
alarmPendingIntent = alarmPendingIntent.let { intent ->
PendingIntent.getBroadcast(rootView.context, 0, alarmIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
}
var trig: AlarmManager.AlarmClockInfo =
AlarmManager.AlarmClockInfo(calendar.timeInMillis, alarmPendingIntent)
alarmMgr?.setAlarmClock(
trig,
alarmPendingIntent
)
//below works to set alarm, is in recylerview adapter
var alarmTimeEntity: AlarmTimeEntity
var alarmIntent: Intent
var alarmPendingIntent: PendingIntent
alarmIntent = Intent(context, AlarmReceiver::class.java)
alarmIntent.putExtra("alarmTimeEntityId", alarmTimeEntity.alarmTimeId)
alarmPendingIntent = PendingIntent.getActivity(context, 0, alarmIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
alarmPendingIntent = alarmPendingIntent.let { intent ->
PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_IMMUTABLE
or PendingIntent.FLAG_UPDATE_CURRENT)
}
var trig: AlarmManager.AlarmClockInfo =
AlarmManager.AlarmClockInfo(calendar.timeInMillis, alarmPendingIntent)
alarmMgr?.setAlarmClock(
trig,
alarmPendingIntent
)
CodePudding user response:
I didnt properly intitalize the alarm manager object. I had private var alarmMgr: AlarmManager? = null to declare it. But I needed to also have alarmMgr = this.getSystemService(Context.ALARM_SERVICE) as AlarmManager at some point before setting the alarm to make it work! clutz move. I keep doing that. Its there if anyone else needs it!