初始化驅動程式 SDK

使用 Driver SDK 前,必須先初始化 請按照下列步驟操作:

  1. NavigationApi 取得 Navigator 物件。

    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. 建立 DriverContext 物件,填入必要欄位。如要初始化 DriverContext 物件,您必須輸入 Google Cloud 專案的專案 ID 做為 providerId。資訊 有關設定 Google Cloud 專案的指示,請參閱 建立 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. 使用 DriverContext 物件初始化 *DriverApi

    Java

    RidesharingDriverApi ridesharingDriverApi = RidesharingDriverApi.createInstance(driverContext);
    

    Kotlin

    val ridesharingDriverApi = RidesharingDriverApi.createInstance(driverContext)
    
  4. 從 API 物件取得 RidesharingVehicleReporter。(*VehicleReporter 擴充 NavigationVehicleReporter)。

    Java

    RidesharingVehicleReporter vehicleReporter = ridesharingDriverApi.getRidesharingVehicleReporter();
    

    Kotlin

    val vehicleReporter = ridesharingDriverApi.getRidesharingVehicleReporter()
    

SSL/TLS 注意事項

在內部,Driver SDK 實作會使用 SSL/TLS 與 Fleet Engine 服務安全通訊。Android API 23 以下版本可能需要 SecurityProvider 修補程式才能與伺服器通訊。如要進一步瞭解如何在 Android 中使用 SSL,請參閱「安全性 GMS 供應器」一文。本文也包含修補安全性提供者的程式碼範例。

後續步驟

準備車輛