AI-generated Key Takeaways
-
Use
FenceClient.queryFences()
to get the current state of a fence by providing its fence key. -
The
queryFences()
method returns aFenceStateMap
containing the state of the queried fences. -
FenceState
objects within the map provide the current, previous state, and last update time of each fence. -
The provided code example demonstrates querying for a fence's state and logging its details.
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;
}
});
}