Fix: Implement BootBroadcastReceiver to reschedule alarms after reboot#14
Fix: Implement BootBroadcastReceiver to reschedule alarms after reboot#14itsmeaabhii wants to merge 2 commits into
Conversation
- Added BootBroadcastReceiver to handle BOOT_COMPLETED intent - Added RECEIVE_BOOT_COMPLETED permission to AndroidManifest.xml - Automatically reschedules all active alarms from database on boot - Fixes issue CCExtractor#9
There was a problem hiding this comment.
Pull request overview
This pull request implements a BootBroadcastReceiver to address the issue where alarms fail to ring after device reboot. When an Android device restarts, all pending alarms scheduled via AlarmManager are cleared by the system. This PR adds functionality to automatically reschedule active alarms from the database when the device boots up.
Changes:
- Added BootBroadcastReceiver class to handle device boot events and reschedule alarms
- Added RECEIVE_BOOT_COMPLETED permission to AndroidManifest.xml
- Updated dependency versions in pubspec.lock (transitive dependency updates)
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| android/app/src/main/kotlin/com/example/uac_companion/AlarmServices/BroadcastReceivers/BootBroadcastReceiver.kt | New receiver that listens for boot completion and reschedules enabled alarms from the database |
| android/app/src/main/AndroidManifest.xml | Added RECEIVE_BOOT_COMPLETED permission and registered BootBroadcastReceiver with intent filters for BOOT_COMPLETED and QUICKBOOT_POWERON |
| pubspec.lock | Updated transitive dependencies to newer versions (standard Flutter/Dart SDK updates) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
|
|
||
| override fun onReceive(context: Context, intent: Intent) { | ||
| if (intent.action == Intent.ACTION_BOOT_COMPLETED) { | ||
| Log.d(TAG, "Device boot completed. Rescheduling alarms...") |
There was a problem hiding this comment.
The intent filter in AndroidManifest.xml includes both BOOT_COMPLETED and QUICKBOOT_POWERON actions, but this receiver only handles BOOT_COMPLETED. The QUICKBOOT_POWERON action (used by some manufacturers for quick boot) will not trigger alarm rescheduling. Consider checking for both actions or using a more general approach.
| } | |
| override fun onReceive(context: Context, intent: Intent) { | |
| if (intent.action == Intent.ACTION_BOOT_COMPLETED) { | |
| Log.d(TAG, "Device boot completed. Rescheduling alarms...") | |
| private const val ACTION_QUICKBOOT_POWERON = "android.intent.action.QUICKBOOT_POWERON" | |
| private const val ACTION_HTC_QUICKBOOT_POWERON = "com.htc.intent.action.QUICKBOOT_POWERON" | |
| } | |
| override fun onReceive(context: Context, intent: Intent) { | |
| val action = intent.action | |
| if (action == Intent.ACTION_BOOT_COMPLETED || | |
| action == ACTION_QUICKBOOT_POWERON || | |
| action == ACTION_HTC_QUICKBOOT_POWERON) { | |
| Log.d(TAG, "Device boot completed or quick boot power on. Rescheduling alarms...") |
| // Reschedule each alarm using the AlarmScheduler | ||
| // The scheduler will automatically pick the next upcoming alarm | ||
| if (enabledAlarms.isNotEmpty()) { | ||
| AlarmScheduler.scheduleNextAlarm(context) |
There was a problem hiding this comment.
The comment on line 29 states "Reschedule each alarm using the AlarmScheduler," but the code actually only schedules the next upcoming alarm via scheduleNextAlarm(context). Based on the AlarmScheduler implementation, this only schedules one alarm at a time (the next upcoming one). The comment should be updated to reflect this behavior, or if all enabled alarms should be scheduled, the implementation needs to be changed.
|
|
||
| try { | ||
| // Get all enabled alarms from the database | ||
| val alarms = AlarmUtils.getAllAlarmsFromDb(context) | ||
| val enabledAlarms = alarms.filter { it.isEnabled == 1 } | ||
|
|
||
| Log.d(TAG, "Found ${enabledAlarms.size} enabled alarms to reschedule") | ||
|
|
||
| // Reschedule each alarm using the AlarmScheduler | ||
| // The scheduler will automatically pick the next upcoming alarm | ||
| if (enabledAlarms.isNotEmpty()) { | ||
| AlarmScheduler.scheduleNextAlarm(context) | ||
| Log.d(TAG, "Successfully rescheduled alarms after boot") | ||
| } else { | ||
| Log.d(TAG, "No active alarms to reschedule") | ||
| } |
There was a problem hiding this comment.
The enabledAlarms variable is filtered but never used. Lines 24-27 filter for enabled alarms and log the count, but line 32 calls scheduleNextAlarm which internally filters again for enabled alarms. Consider removing the unused variable or using it to avoid redundant filtering.
| try { | |
| // Get all enabled alarms from the database | |
| val alarms = AlarmUtils.getAllAlarmsFromDb(context) | |
| val enabledAlarms = alarms.filter { it.isEnabled == 1 } | |
| Log.d(TAG, "Found ${enabledAlarms.size} enabled alarms to reschedule") | |
| // Reschedule each alarm using the AlarmScheduler | |
| // The scheduler will automatically pick the next upcoming alarm | |
| if (enabledAlarms.isNotEmpty()) { | |
| AlarmScheduler.scheduleNextAlarm(context) | |
| Log.d(TAG, "Successfully rescheduled alarms after boot") | |
| } else { | |
| Log.d(TAG, "No active alarms to reschedule") | |
| } | |
| try { | |
| // Delegate rescheduling to AlarmScheduler; it will handle enabled alarms internally | |
| AlarmScheduler.scheduleNextAlarm(context) | |
| Log.d(TAG, "Reschedule request sent to AlarmScheduler after boot") |
- Implemented dynamic location fetching using fl_location package - Added fetchCurrentLocation() method with permission handling - Replaced hardcoded New Delhi coordinates with real-time user location - Added fallback to default location if permission denied or unavailable - Added loading indicator in UI while fetching location - Graceful error handling with user-friendly snackbar notifications - Fixes issue CCExtractor#6
Fix: Implement BootBroadcastReceiver to reschedule alarms after reboot
Description
This PR fixes the issue where alarms fail to ring after device reboot. Previously, when a Wear OS device restarted, all pending alarms scheduled via AlarmManager were wiped by the Android system, and the app did not automatically reschedule them. This implementation adds a BootBroadcastReceiver that listens for device boot completion and automatically reschedules all active alarms from the SQLite database.
Type of Change
Proposed Changes
BootBroadcastReceiver.ktto handleBOOT_COMPLETEDandQUICKBOOT_POWERONintentsRECEIVE_BOOT_COMPLETEDpermission toAndroidManifest.xmlAlarmScheduler.scheduleNextAlarm()methodFixes #9
Screenshots
N/A - This is a background service fix with no UI changes. The functionality can be tested by:
Checklist