Urgent.News

What's breaking now, across thousands of outlets.

Tech

Architecting a background-service-based sound manager that survives Android's Doze mode

It was the final ten minutes of a high-stakes client presentation. I was mid-sentence, explaining a complex system migration, when my phone erupted with a loud, aggressive ringtone. The room went silent, but my phone did not. I scrambled to silence it, accidentally hitting the volume buttons while fumbling with the screen. That moment of pure, unadulterated embarrassment followed me for days. It…

In the final ten minutes of a crucial client presentation, the speaker's phone suddenly began ringing loudly, disrupting the audience. This embarrassing moment drove the speaker to seek a more reliable solution for managing phone ringer settings in sensitive environments. Most people constantly worry about their devices, checking to ensure volume levels are appropriate before entering movie theaters, religious services, or medical consultations.

Existing solutions were either too manual or too intrusive, requiring constant input from the user or constantly draining the battery.

The speaker wanted a background utility that could work without constant interaction, responding to changes in the user's environment. To achieve this, they needed a background service that could survive Android's power-saving Doze mode. The main challenge was ensuring that the sound-toggling logic fired at the right moment, even if the device had been idle for hours.

Initially, the speaker used a standard Service, but Android's lifecycle management caused it to be killed to save resources. They then tried using a ForegroundService with a persistent notification, which solved visibility but did not address timing accuracy. They ultimately realized they needed to use AlarmManager with setExactAndAllowWhileIdle, allowing the system to wake the device from Doze mode to fire a broadcast and trigger audio manager state changes.

The architecture consisted of scheduling the event via AlarmManager and executing it with a BroadcastReceiver, separating scheduling from execution logic. However, the speaker faced challenges with the volatility of the Do Not Disturb (DND) API and how AlarmManager behaves when the system time changes, such as during a Daylight Savings Time shift. They had to implement frequent checks for DND access permissions and store local time representations, recalculating trigger times when relevant broadcasts were received.

The speaker's biggest lesson was to stop fighting Android's power-management systems and instead work within their constraints. Doze mode is not a bug but a feature that extends battery life. Background execution should be event-driven, using AlarmManager for time-based tasks and WorkManager for periodic synchronization or maintenance. Prioritizing user battery life is crucial, as consuming significant energy will lead to the app being uninstalled, regardless of its usefulness.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Building an expert matching recommender

La forma del problema Un directorio es una superficie: el miembro lo abre y adivina. Un recomendador es una superficie de empujar: el sistema propone y tiene que justificarse.

  • Expert recommendation engine faces challenges due to finite expert capacity
  • Reciprocal Rank Fusion normalizes evaluator outputs for credibility
  • Expert quality saturates at score 0.5, preventing overvaluation

Building PickTool with Next.js and Laravel: Lessons from Creating a Software Discovery Platform

Finding software is easy. Finding the right software is not. Search for almost any category—email marketing, CRM, productivity, design, or AI—and you will find hundreds of options.

  • PickTool is a platform for discovering and comparing AI and SaaS tools.
  • Built with Next.js for the frontend and Laravel for the backend, using MySQL for content storage.
  • Relational data modeling improves user experience and SEO.

More from Monday 24 August →