Query for Fence State

You can query for the current state of a fence by calling FenceApi.queryFences(), and passing the fence key for the fence to query.

The following example shows calling FenceApi.queryFences() to get a FenceStateMap, and then using the FenceStateMap to return a FenceState to show the current state, previous state, and the time when the fence was last updated:

protected void queryFence(final String fenceKey) {
    Awareness.FenceApi.queryFences(mGoogleApiClient,
            FenceQueryRequest.forFences(Arrays.asList(fenceKey)))
            .setResultCallback(new ResultCallback<FenceQueryResult>() {
                @Override
                public void onResult(@NonNull FenceQueryResult fenceQueryResult) {
                    if (!fenceQueryResult.getStatus().isSuccess()) {
                        Log.e(TAG, "Could not query fence: " + fenceKey);
                        return;
                    }
                    FenceStateMap map = fenceQueryResult.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())));
                    }
                }
            });
}

Send feedback about...

assistant
Google Awareness API