如何检查是否为Awareness API注册了fence密钥?

时间:2017-01-02 13:40:31

标签: android google-awareness

我有2个关于Googles Awareness Fences API的问题:

  1. 我们是否有方法检查是否注册了具有给定围栏键的围栏?
  2. 如果我意外地使用相同的围栏键注册2个围栏,会发生什么?

1 个答案:

答案 0 :(得分:1)

  1. 要检查围栏是否已注册,请创建 FenceQueryRequest并检查FenceStateMap是否包含围栏密钥。
  2. 以下是示例代码:

    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();
                        if (!map.getFenceKeys().contains(fenceKey)) {
                          // Fence is not registered. 
                        }
                    }
                });
    }
    
    1. 如果您使用相同的围栏键注册2个围栏,则第二个围栏将替换第一个围栏。请参阅FenceUpdateRequest documentation