It happened during a quiet afternoon at the mosque. The imam was mid-sermon, the room was silent, and then, without warning, my phone decided to belt out an aggressive, high-pitched ringtone. Every head in the room turned. I fumbled for the volume button, my face burning with embarrassment, desperately trying to silence the device while avoiding eye contact. It wasn’t the first time, and it certainly wasn’t the last. That specific moment of social friction became the catalyst for building Muffle, a tool designed to take the cognitive load of sound management off the user.
We all have those places where a ringing phone isn't just annoying; it’s genuinely disruptive. Whether it is a medical appointment, a university lecture, or a place of worship, the social cost of forgetting to toggle your sound profile is high. Most of us rely on our memory to manage this, and human memory is notoriously fallible. I realized that if I wanted a solution, it had to be entirely passive. I couldn't afford to keep opening an app to toggle settings. The automation needed to be invisible, triggering based on context rather than user intervention.
When I started development, I realized the biggest hurdle was the battery. Most Android developers treat the GeofencingClient as a black box, but if you poll GPS data aggressively, the phone's battery life drops like a stone. I had to decide between high-accuracy polling and battery longevity. I chose the Google Play Services Geofencing API because it offloads the monitoring to the system's location hardware, which is significantly more efficient than running a custom LocationListener in the foreground. However, relying on the system API meant I had to manage transition events without waking the CPU unnecessarily.
I architected the engine to use a PendingIntent that wakes up my BroadcastReceiver only when the user crosses the defined geofence boundary. This is the crucial part: I keep the logic inside the receiver minimal. I simply check the event type, verify the fence ID, and push a command to the AudioManager. By keeping the BroadcastReceiver lightweight, the system can kill the process immediately after the task is executed, preventing memory leaks or persistent background battery drain. Here is how I set up the trigger request in my RoutineManager:
kotlin
val geofencingRequest = GeofencingRequest.Builder().apply {
setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
addGeofences(listOf(geofence))
}.build()
val intent = PendingIntent.getBroadcast(
context,
requestCode,
geofenceIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
geofencingClient.addGeofences(geofencingRequest, intent)
This approach works because it leverages the underlying Android hardware optimization. The system is already monitoring location for other processes; by registering my geofence with the API, I am essentially piggybacking on existing system-level location updates. I don't need to request ACCESS_FINE_LOCATION unless absolutely necessary, which helps with user trust and power consumption. The transition from Silent to Normal happens in the background, without the user ever seeing an activity or a splash screen. It just works.
What truly surprised me during testing was the inaccuracy of GPS in dense urban environments. I initially set my geofence radius to 50 meters, thinking it would be precise. In practice, the device often wouldn't trigger the event until I was already halfway through the building, or it would trigger it while I was walking past the building on the street. I learned that relying solely on GPS for geofencing is a mistake in a real-world scenario. I had to implement a buffer zone and add a secondary check against the Location.accuracy property. If the accuracy reported by the system is worse than 100 meters, I treat that transition as unreliable and ignore it.
Another edge case that caught me off guard was Android's 'Doze' mode. If the phone is sitting on a desk for an hour, the system puts the device into a deep sleep, which can delay the geofence transition. I had to ensure my PendingIntent was correctly configured with setAllowWhileIdle(true) on my AlarmManager calls for time-based triggers, but for geofencing, there is little you can do besides request HighAccuracy if you need speed. I eventually learned to accept that geofencing is inherently probabilistic. I added an activity log within the app so users could see exactly when a routine triggered, which turned out to be the most requested feature. It helped users understand why their phone silenced at a specific time, building confidence in the automation.
If I were to start over, I would build a hybrid system that incorporates Wi-Fi SSID detection alongside GPS. Relying only on location coordinates is fragile. If you are in a large building with poor signal, the GPS might jump around, causing your phone to vibrate on and off repeatedly. By adding Wi-Fi fingerprinting, I could create a much more stable trigger for indoor environments where GPS is unreliable. Adding a 'debounce' timer to the logic would also prevent the 'flickering' effect where the phone rapidly switches between sound profiles because of fluctuating location accuracy.
For any developer working with location-based triggers, the biggest lesson is to stop thinking about your code as the center of the universe. The Android operating system is constantly trying to kill your app to save power. You should design your architecture to be 'disposable.' If your app is killed, will the system be able to restart the service and recover the state? If not, you are going to get bug reports about 'missed triggers.' Always save your state to a local database like Room before triggering any action. If the process dies midway, the BroadcastReceiver can pick up the state from the database upon reboot.
Automating the mundane parts of our lives is exactly what these devices were meant for, but the implementation is rarely straightforward. Whether you are dealing with AlarmManager for time-based tasks or GeofencingClient for location, the goal should always be to respect the user's battery life as much as their privacy. At the end of the day, users want their phone to be quiet when it needs to be, without having to think about it. I built Muffle because I got tired of the stress caused by my own device, and I hope this approach to low-power automation helps you build more efficient, reliable tools. You can see how I implemented the rest of the logic or try out the app itself at https://play.google.com/store/apps/details?id=com.muffle.app if you are interested in how the final product performs in the wild.
Top comments (1)
One thing worth adding: geofences registered with GeofencingClient are cleared on device reboot (and in some other cases, like Play services data being cleared), so it helps to re-register them from a BOOT_COMPLETED receiver. Also, as far as I know geofencing still needs ACCESS_FINE_LOCATION, plus background location on Android 10+. Did you manage to avoid it somehow?