আপনার অ্যাপটি অ্যান্ড্রয়েড অটো নাকি অ্যান্ড্রয়েড অটোমোটিভ ওএস-এ চলছে তা নির্ধারণ করতে, রানটাইমে সংযোগের তথ্য পুনরুদ্ধার করার জন্য CarConnection API ব্যবহার করুন। উদাহরণস্বরূপ:
আপনার গাড়ির অ্যাপের
Session, একটিCarConnectionশুরু করুন এবংLiveDataআপডেটের জন্য সাবস্ক্রাইব করুন:কোটলিন
CarConnection(carContext).type.observe(this, ::onConnectionStateUpdated)জাভা
new CarConnection(getCarContext()).getType().observe(this, 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() }জাভা
private void onConnectionStateUpdated(int connectionState) { String message; switch(connectionState) { case CarConnection.CONNECTION_TYPE_NOT_CONNECTED: message = "Not connected to a head unit"; break; case CarConnection.CONNECTION_TYPE_NATIVE: message = "Connected to Android Automotive OS"; break; case CarConnection.CONNECTION_TYPE_PROJECTION: message = "Connected to Android Auto"; break; default: message = "Unknown car connection type"; break; } CarToast.makeText(getCarContext(), message, CarToast.LENGTH_SHORT).show(); }