Awareness API Query for Fence State

2019-06-17 02:08发布

问题:

I am trying to get fence state with the Awareness API. Here is the documentation about this. But... When using:

Awareness.FenceApi.queryFences

I can see its deprecated, and instead need to use:

Awareness.getFenceClient

Can anyone please give me an example how to get fence status with getFenceClient?

回答1:

Got it:

Awareness.getFenceClient(context).queryFences(FenceQueryRequest.forFences(Arrays.asList(key)))
            .addOnSuccessListener(new OnSuccessListener<FenceQueryResponse>() {

                @Override
                public void onSuccess(FenceQueryResponse fenceQueryResponse) {

                    FenceStateMap map = fenceQueryResponse.getFenceStateMap();

                    for (String fenceKey : map.getFenceKeys()) {
                        FenceState fenceState = map.getFenceState(fenceKey);
                        Log.i(TAG, "Fence " + fenceKey + ": "
                                + fenceState.getCurrentState()
                                + ", was="
                                + fenceState.getPreviousState());
                    }
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                    Log.d(TAG, "Failed: " + e);
                }
            });