适用于 Android 4.0 的驱动程序 SDK 版本要求您更新特定操作的代码。本指南概述了具体变化以及迁移代码需要执行的操作。
软件包名称更改
软件包名称已从 com.google.android.libraries.ridesharing.driver
更改为 com.google.android.libraries.mapsplatform.transportation.driver
。请更新代码中的引用。
初始化 SDK
在早期版本中,您需要初始化 Navigation SDK,然后获取对 FleetEngine
类的引用。在驱动程序 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
。RidesharingDriverApi ridesharingDriverApi = RidesharingDriverApi.createInstance(driverContext);
从 API 对象获取
NavigationVehicleReporter
。*VehicleReporter
扩展了NavigationVehicleReporter
。RidesharingVehicleReporter vehicleReporter = ridesharingDriverApi.getRidesharingVehicleReporter();
启用和停用位置更新
在早期版本中,您可以在获取 FleetEngine
引用后启用位置信息更新。在驱动程序 SDK v4 中,按如下方式启用位置信息更新:
RidesharingVehicleReporter reporter = ...;
reporter.enableLocationTracking();
如需更新报告间隔,请使用 RidesharingVehicleReporter.setLocationReportingInterval(long, TimeUnit)
。
司机换档完成后,通过调用 NavigationVehicleReporter.disableLocationTracking()
停用位置信息更新,并将车辆标记为离线。
在服务器上设置车辆状态
在早期版本中,您可以使用 FleetEngine
对象来设置车辆状态。在驱动程序 SDK v4 中,使用 RidesharingVehicleReporter
对象设置车辆状态:
RidesharingVehicleReporter reporter = ...;
reporter.enableLocationTracking();
reporter.setVehicleState(VehicleState.ONLINE);
如需将车辆状态设置为 OFFLINE
,请调用 RidesharingVehicleReporter.disableLocationTracking()
。更新车辆状态的错误是通过 DriverContext
中设置的(可选)提供的 StatusListener
来传播。
使用 StatusListener 进行 Error Reporting
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.
}
}
使用 AuthTokenFactory
进行身份验证
AuthTokenFactory
现在只有一个方法,即 getToken()
,该方法使用 AuthTokenContext
作为参数。废弃了 ServiceType
。现在,您只需对所包含的车辆 ID 进行索赔,而无需依赖于 ServiceType
。
class JsonAuthTokenFactory implements AuthTokenFactory {
private String token; // initially null
private long expiryTimeMs = 0;
// This method is called on a thread whose only responsibility is to send
// location updates. Blocking is OK, but just know that no location updates
// can occur until this method returns.
@Override
public String getToken(AuthTokenContext authTokenContext) {
if (System.currentTimeMillis() > expiryTimeMs) {
// The token has expired, go get a new one.
fetchNewToken(authTokenContext.getVehicleId());
}
return token;
}
private void fetchNewToken(String vehicleId) {
String url = "https://yourauthserver.example/token/" + vehicleId;
try (Reader r = new InputStreamReader(new URL(url).openStream())) {
com.google.gson.JsonObject obj
= com.google.gson.JsonParser.parseReader(r).getAsJsonObject();
token = obj.get("Token").getAsString();
expiryTimeMs = obj.get("TokenExpiryMs").getAsLong();
// The expiry time could be an hour from now, but just to try and avoid
// passing expired tokens, we subtract 10 minutes from that time.
expiryTimeMs -= 10 * 60 * 1000;
} catch (IOException e) {
// It's OK to throw exceptions here. The StatusListener you passed to
// create the DriverContext class is notified, and passes along the failed
// update warning.
throw new RuntimeException("Could not get auth token", e);
}
}
}