Chuẩn bị xe sẵn sàng
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Phần này cho biết cách chuẩn bị xe cho các tác vụ theo lịch. Bạn phải hoàn tất từng bước sau đây trước khi phần phụ trợ có thể so khớp một chiếc xe với một nhiệm vụ.
Thiết lập trình nghe
Vì Driver SDK thực hiện các thao tác ở chế độ nền, hãy sử dụng DriverStatusListener
để kích hoạt thông báo khi xảy ra một số sự kiện nhất định, chẳng hạn như lỗi, cảnh báo hoặc thông báo gỡ lỗi. Lỗi có thể chỉ là tạm thời (chẳng hạn như BACKEND_CONNECTIVITY_ERROR
) hoặc có thể khiến các bản cập nhật vị trí ngừng vĩnh viễn. Ví dụ: nếu bạn nhận được lỗi VEHICLE_NOT_FOUND
, thì lỗi này cho biết có lỗi về cấu hình.
Ví dụ sau đây cho thấy một cách triển khai DriverStatusListener
:
class MyStatusListener implements DriverStatusListener {
/** Called when background status is updated, during actions such as location reporting. */
@Override
public void updateStatus(
StatusLevel statusLevel, StatusCode statusCode, String statusMsg, @Nullable Throwable cause) {
// Existing implementation
if (cause != null && cause instanceof StatusRuntimeException) {
if (Status.NOT_FOUND.getCode().equals(cause.getStatus().getCode())) {
// NOT_FOUND gRPC exception thrown by Fleet Engine.
}
}
}
}
DriverContextBuilder.setDriverStatusListener(new MyStatusListener());
Bật thông báo cập nhật vị trí
Sau khi có một thực thể *VehicleReporter
, hãy bật thông báo cập nhật vị trí như sau:
Java
DeliveryVehicleReporter reporter = ...;
reporter.enableLocationTracking();
Kotlin
val reporter = ...
reporter.enableLocationTracking()
(Không bắt buộc) Đặt khoảng thời gian cập nhật
Theo mặc định, Driver SDK gửi thông tin cập nhật vị trí sau mỗi 10 giây. Mỗi lần cập nhật vị trí cũng cho biết rằng xe đang trực tuyến. Bạn có thể thay đổi khoảng thời gian này bằng reporter.setLocationReportingInterval(long, TimeUnit)
. Khoảng thời gian cập nhật tối thiểu được hỗ trợ là 5 giây. Việc cập nhật thường xuyên hơn có thể khiến các yêu cầu và lỗi xảy ra chậm hơn.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-31 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eBefore tasks can be assigned, vehicles must be prepared by completing setup steps that enable communication with the backend system.\u003c/p\u003e\n"],["\u003cp\u003eA \u003ccode\u003eDriverStatusListener\u003c/code\u003e is implemented to receive notifications regarding the vehicle's status, including errors, warnings, and debug messages.\u003c/p\u003e\n"],["\u003cp\u003eLocation updates are enabled using the \u003ccode\u003eenableLocationTracking()\u003c/code\u003e method of a \u003ccode\u003e*VehicleReporter\u003c/code\u003e instance, ensuring the backend knows the vehicle's position.\u003c/p\u003e\n"],["\u003cp\u003eOptionally, the frequency of location updates can be customized, but it's recommended to keep it at the default 10-second interval or above to avoid performance issues.\u003c/p\u003e\n"]]],[],null,["# Get the vehicle ready\n\nThis section shows how to get the vehicle ready for scheduled tasks. You must\ncomplete each of the following steps before your backend can match a vehicle to\na task.\n\nSet up listener\n---------------\n\nSince the Driver SDK performs actions in the\nbackground, use the `DriverStatusListener` to trigger notifications when certain\nevents occur, such as errors, warnings, or debug messages. Errors can be\ntransient in nature (such as `BACKEND_CONNECTIVITY_ERROR`), or they might\ncause location updates to stop permanently. For example, if you receive a\n`VEHICLE_NOT_FOUND` error, it indicates a configuration error.\n\nThe following example shows a `DriverStatusListener` implementation: \n\n class MyStatusListener implements DriverStatusListener {\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, @Nullable Throwable cause) {\n // Existing implementation\n\n if (cause != null && cause instanceof StatusRuntimeException) {\n if (Status.NOT_FOUND.getCode().equals(cause.getStatus().getCode())) {\n // NOT_FOUND gRPC exception thrown by Fleet Engine.\n }\n }\n }\n }\n\n DriverContextBuilder.setDriverStatusListener(new MyStatusListener());\n\nEnable location updates\n-----------------------\n\nOnce you have a `*VehicleReporter` instance, enable location updates as follows: \n\n### Java\n\n DeliveryVehicleReporter reporter = ...;\n\n reporter.enableLocationTracking();\n\n### Kotlin\n\n val reporter = ...\n\n reporter.enableLocationTracking()\n\n### (Optional) Set the update interval\n\nBy default, the Driver SDK sends location updates at 10-second intervals. Each\nlocation update also indicates that the vehicle is online. You can change this\ninterval with\n`reporter.setLocationReportingInterval(long, TimeUnit)`. The minimum supported\nupdate interval is 5 seconds. More frequent updates may result in slower\nrequests and errors."]]