惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
A
About on SuperTechFans
Martin Fowler
Martin Fowler
M
MIT News - Artificial intelligence
Recent Announcements
Recent Announcements
D
DataBreaches.Net
B
Blog
博客园 - 【当耐特】
爱范儿
爱范儿
有赞技术团队
有赞技术团队
P
Proofpoint News Feed
WordPress大学
WordPress大学
小众软件
小众软件
Apple Machine Learning Research
Apple Machine Learning Research
I
InfoQ
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
Microsoft Azure Blog
Microsoft Azure Blog
雷峰网
雷峰网
量子位
G
Google Developers Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Architecting Reliable GPS Triggers in Android Despite Doz...
Haseeb · 2026-06-25 · via DEV Community

Haseeb

We have all been there: you walk into a meeting or a quiet space, expecting your phone to silence itself, only to realize the GPS trigger failed because the system put your app to sleep. As I built Muffle, an Android utility for automated sound profiles, I quickly realized that standard background location polling is insufficient for the modern Android ecosystem.

The Problem: Doze Mode vs. Real-time Needs

Android’s Doze Mode is essential for battery health, but it is the enemy of background tasks that rely on precise location updates. If your app attempts to poll GPS while the device is stationary and the screen is off, the system will batch those requests or kill them entirely. I initially tried using a simple Foreground Service with a LocationListener, but I found that on certain OEMs, even that was getting throttled to save power.

The Solution: Combining FusedLocationProvider with WorkManager

To solve this without draining the user's battery, I shifted my architecture. Instead of a constant location stream, I utilized the FusedLocationProviderClient with a GeofencingClient. By defining physical geofences around high-priority areas (like a workplace or a place of worship), I offload the heavy lifting to the Google Play Services location hardware.

kotlin
val geofenceRequest = GeofencingRequest.Builder().apply {
addGeofences(geofenceList)
setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
}.build()

When a geofence is triggered, the OS sends a PendingIntent to a BroadcastReceiver. Because geofencing events are handled by the system, this bypasses the standard Doze Mode restrictions on polling. If the geofence event requires a more complex calculation (like checking a calendar event or prayer time), I hand the logic off to WorkManager with ExistingPeriodicWorkPolicy.KEEP to ensure the task eventually executes even if the device was in a deep sleep state.

A Necessary Tradeoff

The tradeoff here is responsiveness. Geofencing relies on the system's approximation of your location, which can sometimes lag by a few hundred meters or a few seconds depending on whether the phone has a clear view of the sky. I opted for this over a high-frequency polling service because it respects the user's battery—a non-negotiable for a utility app.

Building Muffle has taught me that on Android, the best background architecture isn't the one that fights the OS for resources, but the one that uses the platform's native hooks to work in harmony with power-saving states. By leveraging the system's own geofencing engine, I managed to create a reliable silent-mode trigger that doesn't sacrifice the user's battery life.