Query for fence state
Stay organized with collections
Save and categorize content based on your preferences.
To query for the current state of a fence, call
FenceClient.queryFences()
and pass the fence key for the fence to query.
The following example calls FenceClient.queryFences()
to get a FenceStateMap
,
and then uses the FenceStateMap
to return a
FenceState
value to show the current state, previous state, and the time when the fence was
last updated:
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;
}
});
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-28 UTC.
[null,null,["Last updated 2025-08-28 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 }"]]