Engineering

Optimizing GPS Precision for Static Utility Apps

Learn how to fetch high-precision location data for static apps without draining the battery, using lessons from Salah Companion.

Amin Shekh · November 25, 2026
Share

Developing utility apps that rely on geographic data often presents a specific engineering paradox. On one hand, your calculations—such as determining prayer times or local regulatory zones—might require high-precision coordinates to be accurate to the minute or the meter. On the other hand, unlike a fitness tracker or a navigation app, your app is effectively 'static.' It does not need to know where the user is every ten seconds; it only needs a highly accurate snapshot when the user changes cities or manually refreshes their settings. In building Salah Companion: Prayer Times, we had to solve this exact problem: providing sub-degree accuracy for astronomical calculations without the persistent battery drain associated with active GPS polling.

The Problem with 'Last Known Location'

Many developers reach for the getLastLocation() method in Google Play Services as a battery-safe default. While this is the most energy-efficient way to get coordinates, it is often insufficient for apps that require high precision. The 'last known location' is simply a cached value from the last time any app on the device successfully requested location. If the user has been inside a large building for three hours, or if they just stepped off a plane and haven't opened a map app yet, that cached value could be kilometers away from their actual position.

For an app like Salah Companion, a discrepancy of even 50 kilometers can shift prayer times by several minutes, potentially leading to inaccurate reminders. We realized that relying solely on cached data was a risk to our core value proposition. We needed a 'Freshness First' approach that triggers a high-power sensor only when necessary, then shuts it down immediately.

The Precision-Interval Framework

To balance these needs, we implemented a tiered fetching strategy. Instead of a single call, we treat location acquisition as a three-step waterfall process. This ensures we never use more power than required, but we also never settle for 'stale' data.

  1. 1.The Cache Check: Check the last known location. If the timestamp is less than 30 minutes old and the accuracy radius is under 100 meters, use it.
  2. 2.The Passive Listener: If the cache is stale, we start a passive location request. This 'piggybacks' on other apps. If the user opens a weather app or a map while our service is waiting, we grab that high-accuracy update for free.
  3. 3.The Active Burst: If no location is found within a 10-second window, we request a single 'Priority High Accuracy' update. We set a strict timeout and a small displacement filter to ensure the GPS hardware doesn't stay active longer than 15 seconds.

Why FusedLocationProviderClient is Essential

On Android, using the raw LocationManager is rarely the right choice for static apps. The FusedLocationProviderClient handles the complex logic of switching between GPS (high power, high accuracy), Wi-Fi/Cellular (medium power, medium accuracy), and sensors like the accelerometer to determine if the device is actually moving.

In our implementation, we leverage the getCurrentLocation() method introduced in newer API levels, which is specifically designed for this 'single shot' use case. Unlike requestLocationUpdates(), which requires you to manually call removeUpdates(), getCurrentLocation() is a Task-based API that returns a single result and automatically manages the sensor lifecycle. This prevents one of the most common battery-drain bugs: 'leaking' a GPS listener because the stop command was never reached due to an app crash or a lifecycle interruption.

Comparison: Location Request Strategies

StrategyPower CostAccuracyBest Use Case
Last KnownNear ZeroHighly VariableGeneral weather, timezone detection
Priority BalancedLow~40m - 100mRegional settings, city-level services
Priority High AccuracyHigh (Short Burst)~1m - 10mPrayer times, Qibla, exact surveying
Continuous TrackingVery HighConstant <10mTurn-by-turn navigation, running apps

For a utility app, your goal is to stay in the top three rows. You should only ever enter the 'High Accuracy' tier if the 'Balanced' tier fails to meet your precision threshold. In Salah Companion, we found that 'Balanced' (which uses Wi-Fi and Cell towers) is often sufficient for 90% of urban users. We only trigger the GPS hardware when the user is in a remote area or specifically requests a 'Precision Refresh' for the Qibla compass.

Handling the 'Cold Start' GPS Problem

When a device hasn't used GPS for a while, it suffers from a 'cold start' where it needs to download satellite almanac data. This can take up to a minute, during which the user is staring at a loading spinner. To avoid this, we use Designing Defensive UI for Unreliable External APIs principles.

Instead of blocking the UI, we show the user the times based on their last known location with a 'Searching...' status indicator. If the high-accuracy fix arrives, we silently update the data. If it fails (due to being indoors), we provide a manual override. Never let the GPS hardware hold your UI hostage.

Code Implementation Logic

When implementing this, your LocationRequest should be configured with a 'duration'. This is a safety valve. If the GPS cannot get a lock within 20 seconds—perhaps because the user is in a basement—the request should automatically time out.

``kotlin val locationRequest = LocationRequest.Builder( Priority.PRIORITY_HIGH_ACCURACY, 1000 // We want it now, so 1 second interval ).apply { setDurationMillis(15000) // Hard stop after 15 seconds setMaxUpdates(1) // We only want one good fix }.build() ``

By setting setMaxUpdates(1), the system knows to spin down the GPS radio the moment a valid coordinate is received. This is the difference between a 1% battery hit and a 0.01% battery hit over the course of a day.

The Geographic Context of Precision

Precision isn't just about the numbers; it's about what you do with them. In our work on Alif2Ayah, we explored how location data can provide context for historical sites mentioned in text. In that scenario, as with Salah Companion, the coordinates are used as keys for a local lookup table or a specialized astronomical algorithm.

If your app uses the location to fetch data from an API—like checking stock compliance in ShariahSignal—you should also cache the results based on a 'Geofence Lite' approach. If the user has only moved 500 meters, do you really need to re-fetch the data? Probably not. By implementing a distance-based threshold, you save not just battery, but also user data and server costs.

Checklist for Engineering Static Location Apps

  • Define Your Threshold: Do you need 10-meter accuracy or 5-kilometer accuracy? Don't ask for more than you need.
  • Implement a Timeout: Never allow a location request to run indefinitely. 15-20 seconds is usually the sweet spot for a single fix.
  • Check Permission States: Ensure you handle the 'Approximate' permission introduced in Android 12. If the user denies 'Precise' location, your app must be able to gracefully degrade to city-level data.
  • Background Limitations: Remember that background location access is highly restricted. If your app needs to update while the screen is off, you likely need a Foreground Service with the location type declared in your manifest.
  • User Transparency: Use a 'Location Pulse' animation or a simple text update to show the user that the app is actively working to find them. This reduces perceived latency.

What to do next

  1. 1.Audit your Current Requests: Search your codebase for requestLocationUpdates and check if you are properly calling removeUpdates. If you only need a single fix, migrate to getCurrentLocation().
  2. 2.Add a Freshness Filter: Implement a check on your location objects. If location.time is older than your desired threshold, trigger a refresh instead of relying on the cached object.
  3. 3.Test in 'Airplane Mode': Turn off your data and GPS and see how your UI reacts. If it hangs or crashes, implement a defensive timeout and a fallback to the last known 'good' coordinates.
#android#engineering#gps#battery-optimization

One email per launch. No spam, unsubscribe any time.