初始化驱动程序 SDK

在使用 Driver SDK 之前,您必须先按照以下步骤初始化 Navigation 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 作为 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 备注

在内部,驱动程序 SDK 实现使用 与 Fleet Engine 服务安全通信的 SSL/TLS。Android API 23 或更低版本可能需要 SecurityProvider 补丁才能与服务器通信。如需详细了解如何在 Android 中使用 SSL,请参阅 Security GMS Provider。本文还包含用于为安全提供程序打补丁的代码示例。

后续步骤

准备好车辆