查询栅栏状态
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如需查询栅栏的当前状态,请调用 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 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\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 }"]]