04
Dev.to Kotlin
作者:Haseeb
發布:07-10 23:24
Architecting a Background-Controlled Sound Manager That Survives Doze Mode
Stop fighting the OS and build within its constraints.
重點摘要
Use a Foreground Service with AlarmManager for reliable wake-ups.
Handle silent mode, Do Not Disturb access, and geofencing carefully.
Store routine state persistently and rebuild it after reboot.
This article describes the design of Muffle, an Android app that automatically switches sound modes in context-aware situations such as meetings, lectures, and other quiet environments. The author explains why simple background approaches fail under Doze mode and App Standby, and why a Foreground Service paired with AlarmManager setExactAndAllowWhileIdle is needed for reliable scheduling. It also covers the need for NotificationPolicyAccess to change DND-restricted audio settings, the move from LocationManager to GeofencingClient to reduce battery drain, and the use of Room to restore routine state after reboot. The post closes with lessons on using WorkManager, AlarmManager, Hilt, and java.time to build background-heavy Android apps that respect battery life and system limits.
构建一个能在 Doze 模式下存活的后台控制声音管理器
不要与操作系统对抗,而要在它的限制内工作。
重點摘要
用 Foreground Service 配合 AlarmManager 实现可靠唤醒。
处理静音、勿扰权限和地理围栏触发。
用数据库保存状态,重启后重建调度。
本文讲述作者开发 Android 应用 Muffle 的过程:它会在会议、课堂等场景中自动切换手机声音状态。作者发现,单靠 BroadcastReceiver 或 Handler 无法应对 Doze mode 和 App Standby 的限制,因此采用 Foreground Service 结合 AlarmManager 的 setExactAndAllowWhileIdle 来做精确唤醒。文章还提到,修改铃声模式需要 NotificationPolicyAccess 权限;地理围栏应使用 GeofencingClient 以减少电量消耗;同时借助 Room 持久化保存例程状态,并在开机后重建逻辑。最后作者建议使用 WorkManager、Hilt 和 java.time 等工具,顺应系统约束,避免让应用成为耗电负担。
打造一個能在 Doze 模式下存活的背景控制音效管理器
不要再對抗作業系統,而是學會在它的限制內運作。
重點摘要
以 Foreground Service 搭配 AlarmManager 取得可靠喚醒。
處理靜音模式、勿擾權限與地理圍欄觸發。
用資料庫保存狀態,重開機後可重建排程。
本文分享作者開發 Android 應用 Muffle 的經驗:讓手機在會議、課堂等情境下自動切換靜音。作者指出,單靠 BroadcastReceiver 或 Handler 無法抵抗 Doze mode 與 App Standby,因此改以 Foreground Service 搭配 AlarmManager 的 setExactAndAllowWhileIdle 來精準喚醒。文中也說明變更鈴聲模式需考慮 NotificationPolicyAccess 權限,地理圍欄則改用 GeofencingClient 以降低耗電,並透過 Room 儲存狀態、在開機後重建工作流程;最後強調應善用 WorkManager、Hilt 與 java.time 等工具,順應系統限制而非硬撐背景常駐。