1

请帮助我理解为什么我设置的意识围栏没有返回 TRUE 状态为“静止”。我在我的手机上运行应用程序,我是“静止”的,我已经让手机“静止”了一段时间,以确保它们不会延迟触发。

注意:我的围栏已成功注册,因此 api 请求等不会有问题。我的广播接收器也永远不会触发。

下面代码的结果是:

I/Awareness: Fence was successfully registered.
I/Awareness: Fence sitting_at_work: 0, was=0

代码:

AwarenessFence sittingFence = DetectedActivityFence.during(DetectedActivityFence.STILL);
            Intent intent = new Intent(getContext(), activityReceiver.class);
            intent.setAction(activityReceiver.ACTION_FENCE);
            myPendingIntent = PendingIntent.getBroadcast(getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            Awareness.getFenceClient(getContext()).updateFences(new FenceUpdateRequest.Builder()
                    .addFence(activityReceiver.KEY_SITTING_AT_WORK, sittingFence, myPendingIntent)
                    .build())
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            Log.i("Awareness", "Fence was successfully registered.");

                            Awareness.getFenceClient(getContext())
                                    .queryFences(FenceQueryRequest.forFences(Arrays.asList(activityReceiver.KEY_SITTING_AT_WORK)))
                                    .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("Awareness", "Fence " + fenceKey + ": "
                                                        + fenceState.getCurrentState()
                                                        + ", was="
                                                        + fenceState.getPreviousState());
                                            }
                                        }
                                    })
                                    .addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            Log.e("Awareness", "Could not query fence: " + activityReceiver.KEY_SITTING_AT_WORK);
                                            return;
                                        }
                                    });

                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.e("Awareness", "Fence could not be registered: " + e);
                        }
                    });

广播接收器 onReceive:

if (TextUtils.equals(ACTION_FENCE, intent.getAction())) {
            FenceState fenceState = FenceState.extract(intent);
            Log.d("Awareness", "Detected fence action"); }
4

0 回答 0