সংযোগ API

আপনার অ্যাপটি Android Auto নাকি Android Automotive OS-এ চলছে তা নির্ধারণ করতে, রানটাইমে সংযোগের তথ্য পুনরুদ্ধার করতে CarConnection API ব্যবহার করুন। উদাহরণস্বরূপ:

  1. আপনার কার অ্যাপের Session এ, একটি CarConnection শুরু করুন এবং LiveData আপডেটগুলিতে সাবস্ক্রাইব করুন:

    কোটলিন

    CarConnection(carContext).type.observe(this, ::onConnectionStateUpdated)
    

    জাভা

    new CarConnection(getCarContext()).getType().observe(this, this::onConnectionStateUpdated);
    
  2. পর্যবেক্ষকের ক্ষেত্রে, সংযোগ অবস্থার পরিবর্তনের প্রতিক্রিয়া দেখান:

    কোটলিন

    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();
    }