初始化驅動程式 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 物件,您必須輸入專案 ID 也就是 Google Cloud 專案的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

    DeliveryDriverApi driverApi = DeliveryDriverApi.createInstance(driverContext);
    
  4. 從 API 物件取得 DeliveryVehicleReporter。 (DeliveryVehicleReporter 擴充 NavigationVehicleReporter)。

    DeliveryVehicleReporter vehicleReporter = driverApi.getDeliveryVehicleReporter();
    

SSL/TLS 注意事項

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

後續步驟

準備車輛