使用 Driver SDK 前,您必須先按照下列步驟初始化 Navigation SDK 和 Driver 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
。如要瞭解如何設定 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()
使用
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 與 Fleet Engine 服務安全通訊。Android API 23 以下版本可能需要 SecurityProvider
修補程式才能與伺服器通訊。如要進一步瞭解如何在 Android 中使用 SSL,請參閱「安全性 GMS 供應器」一文。本文也包含用於修補安全性提供者的程式碼範例。