เตรียมยานพาหนะให้พร้อม
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ส่วนนี้แสดงวิธีเตรียมยานพาหนะสำหรับงานที่กำหนดเวลาไว้ คุณต้อง
ทำตามขั้นตอนต่อไปนี้ให้ครบถ้วนก่อนที่แบ็กเอนด์จะจับคู่ยานพาหนะกับ
งานได้
ตั้งค่าผู้ฟัง
เนื่องจาก Driver SDK จะดำเนินการในเบื้องหลัง ให้ใช้ DriverStatusListener
เพื่อทริกเกอร์การแจ้งเตือนเมื่อเกิดเหตุการณ์บางอย่าง เช่น ข้อผิดพลาด คำเตือน หรือข้อความแก้ไขข้อบกพร่อง ข้อผิดพลาดอาจ
เกิดขึ้นชั่วคราว (เช่น BACKEND_CONNECTIVITY_ERROR
) หรืออาจ
ทำให้การอัปเดตตำแหน่งหยุดลงอย่างถาวร เช่น หากคุณได้รับข้อผิดพลาด
VEHICLE_NOT_FOUND
แสดงว่าเกิดข้อผิดพลาดในการกำหนดค่า
ตัวอย่างต่อไปนี้แสดงการติดตั้งใช้งาน 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());
เปิดใช้การอัปเดตตำแหน่ง
เมื่อมีอินสแตนซ์ *VehicleReporter
แล้ว ให้เปิดใช้การอัปเดตตำแหน่งโดยทำดังนี้
Java
DeliveryVehicleReporter reporter = ...;
reporter.enableLocationTracking();
Kotlin
val reporter = ...
reporter.enableLocationTracking()
(ไม่บังคับ) ตั้งค่าช่วงเวลาการอัปเดต
โดยค่าเริ่มต้น Driver SDK จะส่งการอัปเดตตำแหน่งทุกๆ 10 วินาที การอัปเดตตำแหน่งแต่ละครั้งยังบ่งบอกว่ารถออนไลน์อยู่ด้วย คุณเปลี่ยนช่วงเวลา
นี้ได้ด้วย
reporter.setLocationReportingInterval(long, TimeUnit)
ช่วงเวลาการอัปเดตขั้นต่ำที่รองรับคือ 5 วินาที การอัปเดตบ่อยขึ้นอาจส่งผลให้คำขอช้าลงและเกิดข้อผิดพลาด
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 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."]]