简体   繁体   English

无论状态如何,Google Awareness API中的HeadphoneFence.unplugged都会触发

[英]HeadphoneFence.unplugged in the Google Awareness API fires regardless of the state

I am using a Nexus 5X phone and trying out the Google Awareness API HeadphoneFence.unplugged() https://developers.google.com/android/reference/com/google/android/gms/awareness/fence/HeadphoneFence 我正在使用Nexus 5X手机并尝试使用Google Awareness API HeadphoneFence.unplugged() https://developers.google.com/android/reference/com/google/android/gms/awareness/fence/HeadphoneFence

I found that it fires my pending intent when the fence is first added, then no matter I plug in or unplug the headphone, it fires up even it supposes to only fire for unplugging. 我发现,第一次添加围栏时,它会触发我的未决意图,然后无论我插入或拔出耳机,它都将触发,即使它只是为拔出插头而触发。

My code is not that interest since it's straight out from the guide. 我的代码不是很有趣,因为它直接来自指南。

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

@Herman It indeed seems thats how it behaves. @Herman似乎的确是这样。 It gives you the state of the headphones when the application is launched, (when fence / receiver is registered) and also when you plug/unplug them. 它会在启动应用程序时(注册了围栏/接收器时)以及在您插入/拔下耳机时为您提供耳机的状态。

It makes sense in a way I guess, that if the headphones are connected, you are not unplugging them, hence it fires FenceState.FALSE and when you unplug them, it fires FenceState.TRUE. 从某种程度上讲,我认为这是有道理的,如果连接了耳机,则您没有拔掉它们,因此它会触发FenceState.FALSE,而当您拔下它们时,它将触发FenceState.TRUE。 However you may choose to act upon the desired event in the 但是,您可以选择在

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM