لتحديد ما إذا كان تطبيقك يعمل على Android Auto أو نظام التشغيل Android Automotive،
استخدِم واجهة برمجة التطبيقات CarConnection لاسترداد معلومات الاتصال في وقت التشغيل.
على سبيل المثال:
- في
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() }