Zainicjuj pakiet SDK sterowników

Zanim zaczniesz korzystać z Driver SDK, musisz najpierw zainicjować Navigation SDK i Driver SDK, wykonując te czynności:

  1. Uzyskaj obiekt Navigator z NavigationApi.

    Java

    NavigationApi.getNavigator(
        this, // Activity
        new NavigationApi.NavigatorListener() {
          @Override
          public void onNavigatorReady(Navigator navigator) {
            // Keep a reference to the Navigator (used to configure and start nav)
            this.navigator = navigator;
          }
        }
    );
    

    Kotlin

    NavigationApi.getNavigator(
      this, // Activity
      object : NavigatorListener() {
        override fun onNavigatorReady(navigator: Navigator) {
          // Keep a reference to the Navigator (used to configure and start nav)
          this@myActivity.navigator = navigator
        }
      },
    )
    
  2. Utwórz obiekt DriverContext, wypełniając wymagane pola. Aby zainicjować obiekt DriverContext, musisz wpisać identyfikator projektu Google Cloud jako providerId. Więcej informacji o konfigurowaniu projektu Google Cloud znajdziesz w artykule Tworzenie projektu Fleet Engine.

    Java

    DriverContext driverContext = DriverContext.builder(application)
        .setProviderId(providerId)
        .setVehicleId(vehicleId)
        .setAuthTokenFactory(authTokenFactory)
        .setNavigator(navigator)
        .setRoadSnappedLocationProvider(
            NavigationApi.getRoadSnappedLocationProvider(application))
        .build();
    

    Kotlin

    val driverContext =
      DriverContext.builder(application)
        .setProviderId(providerId)
        .setVehicleId(vehicleId)
        .setAuthTokenFactory(authTokenFactory)
        .setNavigator(navigator)
        .setRoadSnappedLocationProvider(NavigationApi.getRoadSnappedLocationProvider(application))
        .build()
    
  3. Użyj obiektu DriverContext, aby zainicjować *DriverApi.

    Java

    RidesharingDriverApi ridesharingDriverApi = RidesharingDriverApi.createInstance(driverContext);
    

    Kotlin

    val ridesharingDriverApi = RidesharingDriverApi.createInstance(driverContext)
    
  4. Uzyskaj RidesharingVehicleReporter z obiektu API. (*VehicleReporter rozszerza NavigationVehicleReporter.)

    Java

    RidesharingVehicleReporter vehicleReporter = ridesharingDriverApi.getRidesharingVehicleReporter();
    

    Kotlin

    val vehicleReporter = ridesharingDriverApi.getRidesharingVehicleReporter()
    

Uwagi dotyczące SSL/TLS

Wewnętrznie implementacja Driver SDK używa protokołu SSL/TLS do bezpiecznej komunikacji z usługą Fleet Engine. W przypadku wersji Androida 23 lub starszych do komunikacji z serwerem może być wymagana poprawka SecurityProvider. Więcej informacji o pracy z SSL w Androidzie znajdziesz w artykule Security GMS Provider. Artykuł zawiera też przykładowy kod do łatania dostawcy zabezpieczeń.

Co dalej?

Przygotuj pojazd