I have an app that doesn't have any Android Auto/CarPlay features; in other words, it's an ordinary app that wasn't made to run on Android Auto/CarPlay.
With the code below, I thought I could detect when the user connects to Android Auto/CarPlay, but that didn't happen.
Is there a way to know if the user is connected to Android Auto/CarPlay?
carModeReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val action = intent?.action
if (action == Intent.ACTION_DOCK_EVENT) {
val dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, -1)
if (dockState == Intent.EXTRA_DOCK_STATE_CAR) {
updateStatus("Connected to Android Auto/CarPlay")
} else {
updateStatus("Not in car")
}
}
}
}
You can use the CarConnection
API to detect when Android Auto is running. As of Android 12, Android Auto no longer changes the UI mode of the phone when running since there is no phone-screen UI for Android Auto - that is why the method you're attempting doesn't work.