Untitled

mail@pastecode.io avatar
unknown
plain_text
21 days ago
791 B
3
Indexable
Never
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
    if (!alarmManager.canScheduleExactAlarms()) {
        val intent = Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM)
        startActivity(intent)
    } else {
        setExactAlarm(alarmManager, calendar.timeInMillis, alarmPendingIntent)
    }
} else {
    val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
    setExactAlarm(alarmManager, calendar.timeInMillis, alarmPendingIntent)
}

fun setExactAlarm(alarmManager: AlarmManager, timeInMillis: Long, alarmPendingIntent: PendingIntent) {
    alarmManager.setExact(
        AlarmManager.RTC_WAKEUP,
        timeInMillis,
        alarmPendingIntent
    )
}
Leave a Comment