การค้นหาสถานะรั้ว
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หากต้องการค้นหาสถานะปัจจุบันของรั้ว ให้เรียกใช้ FenceClient.queryFences()
และส่งคีย์รั้วสำหรับรั้วที่จะค้นหา
ตัวอย่างต่อไปนี้เรียกใช้ FenceClient.queryFences()
เพื่อรับ FenceStateMap
แล้วใช้ FenceStateMap
เพื่อแสดงค่า FenceState
เพื่อแสดงสถานะปัจจุบัน สถานะก่อนหน้า และเวลาที่รั้วได้รับการอัปเดตล่าสุด
protected void queryFence(final String fenceKey) {
Awareness.getFenceClient(this)
.queryFences(FenceQueryRequest.forFences(Arrays.asList(fenceKey)))
.addOnSuccessListener(new OnSuccessListener<FenceQueryResponse>() {
@Override
public void onSuccess(FenceQueryResponse response) {
FenceStateMap map = response.getFenceStateMap();
for (String fenceKey : map.getFenceKeys()) {
FenceState fenceState = map.getFenceState(fenceKey);
Log.i(TAG, "Fence " + fenceKey + ": "
+ fenceState.getCurrentState()
+ ", was="
+ fenceState.getPreviousState()
+ ", lastUpdateTime="
+ DATE_FORMAT.format(
new Date(fenceState.getLastFenceUpdateTimeMillis())));
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Could not query fence: " + fenceKey);
return;
}
});
}
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\u003cp\u003eUse \u003ccode\u003eFenceClient.queryFences()\u003c/code\u003e to get the current state of a fence by providing its fence key.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003equeryFences()\u003c/code\u003e method returns a \u003ccode\u003eFenceStateMap\u003c/code\u003e containing the state of the queried fences.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eFenceState\u003c/code\u003e objects within the map provide the current, previous state, and last update time of each fence.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code example demonstrates querying for a fence's state and logging its details.\u003c/p\u003e\n"]]],[],null,["# Query for fence state\n\nTo query for the current state of a fence, call\n[`FenceClient.queryFences()`](/android/reference/com/google/android/gms/awareness/FenceClient#public-taskfencequeryresponse-queryfences-fencequeryrequest-fencequeryrequest) and pass the fence key for the fence to query.\n\nThe following example calls `FenceClient.queryFences()` to get a `FenceStateMap`,\nand then uses the `FenceStateMap` to return a\n[`FenceState`](/android/reference/com/google/android/gms/awareness/fence/FenceState)\nvalue to show the current state, previous state, and the time when the fence was\nlast updated: \n\n protected void queryFence(final String fenceKey) {\n Awareness.getFenceClient(this)\n .queryFences(FenceQueryRequest.forFences(Arrays.asList(fenceKey)))\n .addOnSuccessListener(new OnSuccessListener\u003cFenceQueryResponse\u003e() {\n @Override\n public void onSuccess(FenceQueryResponse response) {\n FenceStateMap map = response.getFenceStateMap();\n for (String fenceKey : map.getFenceKeys()) {\n FenceState fenceState = map.getFenceState(fenceKey);\n Log.i(TAG, \"Fence \" + fenceKey + \": \"\n + fenceState.getCurrentState()\n + \", was=\"\n + fenceState.getPreviousState()\n + \", lastUpdateTime=\"\n + DATE_FORMAT.format(\n new Date(fenceState.getLastFenceUpdateTimeMillis())));\n }\n }\n })\n .addOnFailureListener(new OnFailureListener() {\n @Override\n public void onFailure(@NonNull Exception e) {\n Log.e(TAG, \"Could not query fence: \" + fenceKey);\n return;\n }\n });\n }"]]