Skip to content

Commit

Permalink
Merge pull request #568 from wwqbus/exactAlarmCrashFix
Browse files Browse the repository at this point in the history
Fix for exact alarms not allowed
  • Loading branch information
hannesa2 authored Feb 19, 2024
2 parents b48ef71 + fa3ac6e commit 0ea7458
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion serviceLibrary/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
<!-- <uses-permission android:name="android.permission.USE_EXACT_ALARM" /> this is only required for calendar apps -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,27 @@ internal class AlarmPingSender(val service: MqttService) : MqttPingSender {
Timber.d("Schedule next alarm at $nextAlarmInMilliseconds ms")
val alarmManager = service.getSystemService(Service.ALARM_SERVICE) as AlarmManager
pendingIntent?.let {
if (Build.VERSION.SDK_INT >= 23) {
//add check for android 13 and up
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
//check if we can scheduele exact alarms
if (alarmManager.canScheduleExactAlarms()) {
Timber.d("Alarm schedule using setExactAndAllowWhileIdle, next: $delayInMilliseconds")
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextAlarmInMilliseconds, it)
} else // it's up to the app to ask for permission so we schedule with a no exact but we do warn in the logs about it
{
Timber.w("Not allowed to schedule exact alarms! using non exact alarm")
Timber.w("Alarm schedule using setAndAllowWhileIdle, next: $delayInMilliseconds")
alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextAlarmInMilliseconds, it)
}
} else if (Build.VERSION.SDK_INT >= 23) {
// In SDK 23 and above, dosing will prevent setExact, setExactAndAllowWhileIdle will force
// the device to run this task whilst dosing.
Timber.d("Alarm schedule using setExactAndAllowWhileIdle, next: $delayInMilliseconds")
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextAlarmInMilliseconds, it)
} else
} else {
Timber.d("Alarm schedule using setExact, delay: $delayInMilliseconds")
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextAlarmInMilliseconds, it)
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextAlarmInMilliseconds, it)
}
}
}

Expand Down

0 comments on commit 0ea7458

Please sign in to comment.