初始化驱动程序 SDK
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在使用 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
。
DeliveryDriverApi driverApi = DeliveryDriverApi.createInstance(driverContext);
从 API 对象获取 DeliveryVehicleReporter
。
(DeliveryVehicleReporter
扩展了 NavigationVehicleReporter
。)
DeliveryVehicleReporter vehicleReporter = driverApi.getDeliveryVehicleReporter();
SSL/TLS 相关注意事项
在内部,Driver SDK 实现使用 SSL/TLS 与 Fleet Engine 服务安全地通信。Android API 版本 23 或更低版本可能需要 SecurityProvider
补丁才能与服务器通信。如需详细了解如何在 Android 中使用 SSL,请参阅 Security GMS 提供程序。
本文还包含用于修补安全提供程序的代码示例。
后续步骤
准备车辆
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eBefore using the Driver SDK, you must initialize both the Navigation SDK and the Driver SDK.\u003c/p\u003e\n"],["\u003cp\u003eTo begin, obtain a \u003ccode\u003eNavigator\u003c/code\u003e object from the \u003ccode\u003eNavigationApi\u003c/code\u003e and create a \u003ccode\u003eDriverContext\u003c/code\u003e object, ensuring your Google Cloud Project ID is included.\u003c/p\u003e\n"],["\u003cp\u003eInitialize the \u003ccode\u003e*DriverApi\u003c/code\u003e using the \u003ccode\u003eDriverContext\u003c/code\u003e, and obtain the \u003ccode\u003eDeliveryVehicleReporter\u003c/code\u003e from it.\u003c/p\u003e\n"],["\u003cp\u003eNote that the Driver SDK uses SSL/TLS for secure communication, and you might require a \u003ccode\u003eSecurityProvider\u003c/code\u003e patch for older Android versions.\u003c/p\u003e\n"]]],[],null,["# Initialize the Driver SDK\n\nBefore using the Driver SDK, you must first initialize\nthe Navigation SDK and Driver SDK following these steps:\n\n1. Obtain a `Navigator` object from the `NavigationApi`.\n\n ### Java\n\n NavigationApi.getNavigator(\n this, // Activity\n new NavigationApi.NavigatorListener() {\n @Override\n public void onNavigatorReady(Navigator navigator) {\n // Keep a reference to the Navigator (used to configure and start nav)\n this.navigator = navigator;\n }\n }\n );\n\n ### Kotlin\n\n NavigationApi.getNavigator(\n this, // Activity\n object : NavigatorListener() {\n override fun onNavigatorReady(navigator: Navigator) {\n // Keep a reference to the Navigator (used to configure and start nav)\n this@myActivity.navigator = navigator\n }\n },\n )\n\n2. Create a `DriverContext` object, populating the required fields. To\n initialize the `DriverContext` object, you must enter the **Project ID**\n of your Google Cloud Project as the `providerId`. For information\n on setting up the Google Cloud Project, see\n [Create your Fleet Engine project](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/create-project).\n\n ### Java\n\n DriverContext driverContext = DriverContext.builder(application)\n .setProviderId(providerId)\n .setVehicleId(vehicleId)\n .setAuthTokenFactory(authTokenFactory)\n .setNavigator(navigator)\n .setRoadSnappedLocationProvider(\n NavigationApi.getRoadSnappedLocationProvider(application))\n .build();\n\n ### Kotlin\n\n val driverContext =\n DriverContext.builder(application)\n .setProviderId(providerId)\n .setVehicleId(vehicleId)\n .setAuthTokenFactory(authTokenFactory)\n .setNavigator(navigator)\n .setRoadSnappedLocationProvider(NavigationApi.getRoadSnappedLocationProvider(application))\n .build()\n\n3. Use the `DriverContext` object to initialize the `*DriverApi`.\n\n DeliveryDriverApi driverApi = DeliveryDriverApi.createInstance(driverContext);\n\n4. Obtain the `DeliveryVehicleReporter` from the API object.\n (`DeliveryVehicleReporter` extends `NavigationVehicleReporter`.)\n\n DeliveryVehicleReporter vehicleReporter = driverApi.getDeliveryVehicleReporter();\n\nNotes on SSL/TLS\n----------------\n\nInternally, the Driver SDK implementation uses\nSSL/TLS to communicate securely with the Fleet Engine service. Android API\nversions 23 or\nearlier may require a `SecurityProvider` patch to communicate with the\nserver. For more information about working with SSL in Android, see\n[Security GMS Provider](https://developer.android.com/training/articles/security-gms-provider).\nThe article also contains code samples for patching the security provider.\n\nWhat's next\n-----------\n\n[Get the vehicle ready](/maps/documentation/mobility/driver-sdk/scheduled/android/vehicle-ready)"]]