Android Driver SDK 4.0 遷移指南
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 4.0 版 Driver SDK 需要更新特定作業的程式碼。本指南將說明相關異動,以及遷移程式碼時需要採取的行動。
變更套件名稱
套件名稱已從 com.google.android.libraries.ridesharing.driver
變更為 com.google.android.libraries.mapsplatform.transportation.driver
。請更新程式碼中的參照。
初始化 SDK
在舊版中,您會先初始化 Navigation SDK,然後取得 FleetEngine
類別的參照。在 Driver SDK v4 中,請按照下列步驟初始化 SDK:
從 NavigationApi
取得 Navigator
物件。
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;
}
}
);
建立 DriverContext
物件,並填入必填欄位。
DriverContext driverContext = DriverContext.builder(application)
.setProviderId(providerId)
.setVehicleId(vehicleId)
.setAuthTokenFactory(authTokenFactory)
.setNavigator(navigator)
.setRoadSnappedLocationProvider(
NavigationApi.getRoadSnappedLocationProvider(application))
.build();
使用 DriverContext
物件初始化 *DriverApi
。
從 API 物件取得 NavigationVehicleReporter
。
*VehicleReporter
extends NavigationVehicleReporter
。
啟用及停用位置資訊更新
在舊版中,您會在取得 FleetEngine
參照後啟用位置資訊更新。在 Driver SDK 第 4 版中,請按照下列步驟啟用位置資訊更新:
駕駛人結束班表後,請呼叫 NavigationVehicleReporter.disableLocationTracking()
停用位置資訊更新,並將車輛標示為離線。
使用 StatusListener 進行錯誤報告
ErrorListener
已移除並與 StatusListener
合併,定義方式可能如下:
class MyStatusListener implements StatusListener {
/** Called when background status is updated, during actions such as location reporting. */
@Override
public void updateStatus(
StatusLevel statusLevel, StatusCode statusCode, String statusMsg) {
// Status handling stuff goes here.
// StatusLevel may be DEBUG, INFO, WARNING, or ERROR.
// StatusCode may be DEFAULT, UNKNOWN_ERROR, VEHICLE_NOT_FOUND,
// BACKEND_CONNECTIVITY_ERROR, or PERMISSION_DENIED.
}
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-05 (世界標準時間)。
[null,null,["上次更新時間:2025-09-05 (世界標準時間)。"],[[["\u003cp\u003eDriver SDK for Android v4 requires code updates for operations like package name and initialization.\u003c/p\u003e\n"],["\u003cp\u003eThe package name has changed to \u003ccode\u003ecom.google.android.libraries.mapsplatform.transportation.driver\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eSDK initialization now involves obtaining a \u003ccode\u003eNavigator\u003c/code\u003e, creating a \u003ccode\u003eDriverContext\u003c/code\u003e, and initializing the \u003ccode\u003e*DriverApi\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eLocation updates are enabled and disabled using the \u003ccode\u003eNavigationVehicleReporter\u003c/code\u003e methods.\u003c/p\u003e\n"],["\u003cp\u003eError reporting is now handled through the \u003ccode\u003eStatusListener\u003c/code\u003e which replaces the \u003ccode\u003eErrorListener\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["The Driver SDK for Android 4.0 release requires that you update your code\nfor certain operations. This guide outlines the changes and what\nyou'll need to do to migrate your code.\n\nPackage name change\n\nThe package name has changed from\n`com.google.android.libraries.ridesharing.driver` to\n`com.google.android.libraries.mapsplatform.transportation.driver`. Please\nupdate references in your code.\n\nInitializing the SDK\n\nIn earlier versions, you would initialize the Navigation SDK and then obtain\na reference to the `FleetEngine` class. In Driver SDK\nv4, initialize the SDK as follows:\n\n1. Obtain a `Navigator` object from the `NavigationApi`.\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\n2. Create a `DriverContext` object, populating the required fields.\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\n3. Use the `DriverContext` object to initialize the `*DriverApi`.\n\n4. Obtain the `NavigationVehicleReporter` from the API object.\n `*VehicleReporter` extends `NavigationVehicleReporter`.\n\nEnabling and disabling location updates\n\nIn earlier versions, you would enable location updates after obtaining\na `FleetEngine` reference. In Driver SDK v4, enable\nlocation updates as follows:\n\nWhen the driver's shift is finished, disable location updates\nand mark the vehicle as offline by calling `NavigationVehicleReporter.disableLocationTracking()`.\n\nError Reporting with StatusListener\n\n`ErrorListener` has been removed and combined with `StatusListener`,\nwhich may be defined like the following: \n\n class MyStatusListener implements StatusListener {\n /** Called when background status is updated, during actions such as location reporting. */\n @Override\n public void updateStatus(\n StatusLevel statusLevel, StatusCode statusCode, String statusMsg) {\n // Status handling stuff goes here.\n // StatusLevel may be DEBUG, INFO, WARNING, or ERROR.\n // StatusCode may be DEFAULT, UNKNOWN_ERROR, VEHICLE_NOT_FOUND,\n // BACKEND_CONNECTIVITY_ERROR, or PERMISSION_DENIED.\n }\n }"]]