Tracking a school bus from an Android phone: what background location really takes

2026-12-02来源 dev.toEnglish

Vainu tracks Indian school buses with the conductor's Android phone instead of a GPS box. Parents see the bus move during the trip, so the phone has to report its position reliably for the whole trip, often with the screen off, on budget devices from many manufacturers. Here is what that took, beyond calling the location API.

1. A foreground service, and a notification people can see

Background location on modern Android means a foreground service with a visible notification. Two details caught us:

  • On Android 13+ the service's notification is hidden until the app is allowed to post notifications. The conductor then cannot see that location is being shared, and some OEM battery managers treat a silent location service as fair game to kill. We ask for the notification permission once, right before a trip starts, when the reason is obvious.
  • The phone's own Location switch overrides every permission. If it is off, no app gets a position. We check it before the trip and deep-link to the system Location settings page if needed.

2. Ask for background location properly

Google Play requires a prominent disclosure before the background location prompt, and users read it. We ask for foreground location first, show our own disclosure screen explaining that the bus position is shared only during a trip, then request background access. If the conductor declines, we remember the day and do not nag them again until the next day.

3. Ignore the first fix

When tracking starts, Android often delivers its cached last position first. It can be hours old and kilometres away. We record when tracking started and drop any fix older than that, while keeping fixes that arrive late in a batch (taken during the trip, delivered when the phone woke up).

4. Write first, upload second

Every fix is written to an encrypted local outbox before anything is sent. Uploading is opportunistic:

  • GPS points alone go up about every 10 seconds, so parents see a bus that is seconds old.
  • A full sync (attendance, photos, GPS) runs at most about once a minute from the location task.

Separating the two keeps the parent's map fresh without turning every fix into a heavy request.

5. Never invent a position

If the OS delivers no fixes (tunnel, basement, a phone that decided to sleep), we record nothing. The server then reports the bus position as stale, and the parent app shows how old the last position is. We also keep the platform's mocked flag on every point, so spoofed locations are visible rather than silently trusted.

6. Clamp what you store

Accuracy, speed and heading arrive in all sorts of shapes across devices: negative headings, speeds of hundreds of metres a second, missing accuracy. We clamp or drop out-of-range values at the edge so the rest of the system can trust them.

The short version

  • A foreground service with a notification the user can actually see.
  • Notification permission on Android 13+, checked right before tracking.
  • A clear disclosure before background location.
  • Drop stale cached fixes; keep late ones.
  • Persist locally, upload on two cadences.
  • Show staleness instead of inventing positions.

Vainu is school bus tracking and attendance software for Indian schools: vainu.in.

原文:https://www.a8q9.cn/en/article/tracking-a-school-bus-from-an-android-phone-what-background-location-really-take-04c5ee.html

相关文章