Getting the time right in a vehicle is genuinely hard — three different clocks, no network for weeks, a driver who crosses borders, and a timestamp field that fails silently if you pick the wrong one.
Intermediate6 minTime · Clocks · Framework
Time looks like a solved problem until you work on a vehicle. Then you discover
that a car can sit unpowered for six weeks, cross three time zones in an
afternoon, and have no network connection for any of it.
Three clocks, and picking the wrong one is a silent bug#
Android has several notions of "now", and they are not interchangeable.
This is why the topics keep insisting on it. A VehiclePropValue
timestamp is CLOCK_BOOTTIME in nanoseconds — the native equivalent of
elapsedRealtime().
. Satellites broadcast extremely accurate time, and a vehicle
usually has a receiver. This is the best source and needs no network at all.
Network time. If the vehicle is connected, NTP works as it does anywhere.
The . A small battery-backed clock that keeps running while the car
is off. It drifts — seconds per week — but it means the vehicle knows roughly
what time it is the instant it powers on.
The vehicle network. Some vehicles distribute time from a gateway ECU.
A phone changes time zone when its network does. A vehicle drives across borders.
Reacting to a time zone changekotlin
context.registerReceiver( object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { // Everything you have formatted and cached is now wrong. reformatAllDisplayedTimes() } }, IntentFilter(Intent.ACTION_TIMEZONE_CHANGED),)
Whether a vehicle should follow the local zone automatically is a product
decision with genuine arguments on both sides — a driver near a border may not
want the clock flipping back and forth. Check what your programme decided rather
than assuming.
The head unit suspends. An alarm set for 3am has to survive that.
Alarms that fire while the vehicle is asleepkotlin
val alarmManager = context.getSystemService(AlarmManager::class.java)// RTC_WAKEUP wakes the device at a wall-clock timealarmManager.setExactAndAllowWhileIdle( AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent,)// ELAPSED_REALTIME_WAKEUP wakes it after a duration, immune to clock jumpsalarmManager.setExactAndAllowWhileIdle( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delayMillis, pendingIntent,)