【问题标题】:HeadphoneFence.unplugged in the Google Awareness API fires regardless of the state无论状态如何,Google Awareness API 中的 HeadphoneFence.unplugged 都会触发
【发布时间】:2016-07-01 21:39:44
【问题描述】:

我正在使用 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);
                                        }
                                    }
                                });

【问题讨论】:

  • 你能发布完整的代码吗?我会尝试复制它
  • 忘记给你加标签了^^
  • 放入 pastebin:pastebin.com/SYfuit10 如果您需要更多信息,请告诉我。
  • 回调呢?即developers.google.com/awareness/android-api/fence-callbacks 的东西,我无法触发任何回调。我只在 queryFence() 时看到状态
  • 那部分有点混乱。由于 API 允许挂起意图,因此您实际上不需要额外的回调。在我的代码中,我的pendingintent 包装了一个调用BackgroundOneshotService 的intent,它基本上只是打印一条消息并退出,这就是我在注册fence 以及拔下或插入耳机时知道fence 触发的方式。

标签: android google-play-services google-awareness


【解决方案1】:

@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;
            }

【讨论】:

  • 问题是,如果我想发送“你刚刚拔掉手机!”这样的祝酒词,如果我不跟踪我之前检查过的内容,那将无法完成。它不符合围栏的概念,当有事情发生时通知你。如果行为应该是您所描述的,那么另一个栅栏的意义何在:HeadphoneFence.pluggingIn()? developers.google.com/android/reference/com/google/android/gms/…
  • @Herman 好吧,FenceQuery 方法提供了以前的状态,所以如果这对您有任何帮助,您可以使用它,但我理解并同意您的观点
  • 我刚刚发现查询FenceState并没有我想象的那么糟糕。 FenceState.TRUE 并不意味着耳机已拔下。这实际上意味着耳机正在拔出。 FenceState.FALSE 表示它没有拔掉电源。这就是为什么在注册围栏时,围栏状态为 FenceState.FALSE,无论此时耳机是插入还是拔出。简单来说,我只需要查询状态以确保它是 FenceState.TRUE 来发送“拔出”消息。我不需要存储以前的状态等。
猜你喜欢
  • 1970-01-01
  • 2020-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多