フェンスの状態のクエリ
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
フェンスの現在の状態をクエリするには、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 Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-31 UTC。
[null,null,["最終更新日 2025-08-31 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 }"]]