0

I have tried every possible answer on SO but nothing has helped.

I want to reset alarms using AlarmManager on device reboot and the code to do exactly that works, but it doesn't when I put it inside the receiver.

I had tried creating a service but that didn't seem to work at all which that was probably incompetence of my part, however I just can't see why this code isn't working.

AndroidManifest:

<manifest 
(...)

    <uses-permission android:name = "android.permission.RECEIVE_BOOT_COMPLETED" />
(...)

<application
(...)

    <receiver
        android:name = ".utils.DeviceRebootReceiver">
            <intent-filter>
                <action android:name = "android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
    </receiver>

DeviceRebootReceiver:

class DeviceRebootReceiver : BroadcastReceiver() {
    override fun onReceive(context : Context?, intent : Intent?) {
        resetAlarms(context)
    }
}

fun resetAlarms(context:Context):

fun resetAlarms(context : Context?) {
    suspend fun resetAlarmsCoroutine(context : Context) {
        val reminders = NotesDatabase.getInstance(context).notesDatabaseDAO.getAllActiveReminders()
        reminders.forEach {
            if (it.reminderTime > System.currentTimeMillis()) {
                createAlarm(it.reminderTime, it.reminderMessage,null,context)
            }
        }
    }

    CoroutineScope(Dispatchers.IO).launch {
        if (context != null) {
            resetAlarmsCoroutine(context)
        }
    }
}

No need to show the createAlarm() function cause it works fine, BUT since I had seen that AlarmManager could cause problems on reboot I do instantiate it there like so:

val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
Sir
  • 99
  • 1
  • 9
  • Add `` https://stackoverflow.com/a/20441442/3830694 – Krupal Shah Apr 10 '20 at 12:33
  • @KrupalShah that unfortunately also has not worked. I removed the ifelse checking for the intent.action in case I had to add a check for that intent as well, but nope, no sign of that working – Sir Apr 10 '20 at 12:44
  • How exactly have you determined that your Receiver is not getting that broadcast, and that it's not something else failing? Logs? Breakpoints? Something else? – Mike M. Apr 10 '20 at 13:44
  • Well, I tried logs, but they wouldn't appear on logcat, I tried sending a toast but that also didn't do nothing. Impossible to use breakpoints, since the event happens on phone boot, and it's impossible to launch the app in debugging mode on AStudio before that happens. All of that and the fact that the code works on my SystemTimeChange Receiver, so there's no reason for it to not work on this one. – Sir Apr 10 '20 at 14:22
  • "Impossible to use breakpoints..." – You can send that broadcast manually through adb: https://stackoverflow.com/q/11325920. – Mike M. Apr 10 '20 at 15:04
  • Ooh... I did not know about that... I tried that yesterday, but apparently, out of nowhere, my Android Studio 4.0's adb is not working and I can't launch the app in debugging mode. So, I'll reinstall this god forsaken IDE and get back to you if that worked! – Sir Apr 11 '20 at 11:27
  • @MikeM. I do love Android Dev :) tried sending the broadcast, got "Permission Denial", tried to run adb as root, "adbd cannot run as root in production builds" even though my device is rooted and the solutions involve messing with AVD (which I can't run). So much work for something that SHOULD be working – Sir Apr 11 '20 at 14:13

0 Answers0