简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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