0

我正在使用 Nexus 5X 手机并试用 Google Awareness API HeadphoneFence.unplugged() https://developers.google.com/android/reference/com/google/android/gms/awareness/fence/HeadphoneFence

我发现它在第一次添加围栏时触发了我的待处理意图,然后无论我插入或拔出耳机,它都会触发,即使它只是为了拔出而触发。

我的代码不是那么感兴趣,因为它直接来自指南。

Awareness.FenceApi.updateFences(
                                getGoogleApiClient(),
                                new FenceUpdateRequest.Builder()
                                        .addFence(
                                                "something",
                                                HeadphoneFence.unplugging();,
                                                createSendHeadphoneUnpluggedMessagePendingIntent(context))
                                        .build())
                                .setResultCallback(new ResultCallback<Status>() {
                                    @Override
                                    public void onResult(@NonNull Status status) {
                                        if(status.isSuccess()) {
                                            Log.i(TAG, "Headphone unplugged fence was successfully registered.");
                                        } else {
                                            Log.e(TAG, "Headphone unplugged fence could not be registered: " + status);
                                        }
                                    }
                                });
4

1 回答 1

0

@Herman 看起来确实是这样。当应用程序启动时,它会为您提供耳机的状态(注册围栏/接收器时)以及插入/拔出它们时。

我猜在某种程度上是有道理的,如果连接了耳机,你就不会拔掉它们,因此它会触发 FenceState.FALSE,当你拔掉它们时,它会触发 FenceState.TRUE。但是,您可以选择对所需事件采取行动

switch (fenceState.getCurrentState()) {
                case FenceState.TRUE:
                    Log.i(TAG, "Fence > Headphones plugged out");
                    break;
                case FenceState.FALSE:
                    Log.i(TAG, "Fence > Headphones are NOT plugged out.");
                    break;
                case FenceState.UNKNOWN:
                    Log.i(TAG, "Fence > The headphone fence is in an unknown state.");
                    break;
            }
于 2016-07-01T13:39:44.360 回答