适用于 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();
启用和停用位置更新
在早期版本中,您可以在 Google Cloud 控制台
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);
}
}
}