如需确定您的应用是在 Android Auto 上还是在 Android Automotive OS 上运行,
请使用 CarConnection API 在运行时检索连接信息。
例如:
- 在汽车应用的
Session中,初始化CarConnection并订阅LiveData更新:
CarConnection(carContext).type.observe(this, ::onConnectionStateUpdated)
- 在观察器中,对连接状态的变化做出响应:
fun onConnectionStateUpdated(connectionState: Int) { val message = when (connectionState) { CarConnection.CONNECTION_TYPE_NOT_CONNECTED -> "Not connected to a head unit" CarConnection.CONNECTION_TYPE_NATIVE -> "Connected to Android Automotive OS" CarConnection.CONNECTION_TYPE_PROJECTION -> "Connected to Android Auto" else -> "Unknown car connection type" } CarToast.makeText(carContext, message, CarToast.LENGTH_SHORT).show() }