查詢圍欄狀態

如要查詢圍欄目前的狀態,請呼叫 FenceClient.queryFences(),並將 Fencet 鍵用於查詢內容。

下列範例會呼叫 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;
            }
        });
}