在使用驱动程序 SDK 之前,您必须先初始化 Navigation SDK 和驱动程序 SDK:
从
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 } }, )
创建一个
DriverContext
对象,并填充必需字段。如需初始化DriverContext
对象,您必须输入 Google Cloud 项目的项目 ID 作为providerId
。相关信息 请参阅 创建 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()
使用
DriverContext
对象初始化*DriverApi
。Java
RidesharingDriverApi ridesharingDriverApi = RidesharingDriverApi.createInstance(driverContext);
Kotlin
val ridesharingDriverApi = RidesharingDriverApi.createInstance(driverContext)
从 API 对象获取
RidesharingVehicleReporter
。 (*VehicleReporter
扩展了NavigationVehicleReporter
。)Java
RidesharingVehicleReporter vehicleReporter = ridesharingDriverApi.getRidesharingVehicleReporter();
Kotlin
val vehicleReporter = ridesharingDriverApi.getRidesharingVehicleReporter()
SSL/TLS 注意事项
在内部,Driver SDK 实现使用 SSL/TLS 与车队引擎服务进行安全通信。Android API 23 或更低版本可能需要 SecurityProvider
补丁才能与服务器通信。如需详细了解如何在 Android 中使用 SSL,请参阅 Security GMS Provider。本文还包含用于修补安全提供程序的代码示例。