我正在使用Nexus5X手机并尝试使用Google Awareness 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);
}
}
});发布于 2016-07-01 21:39:44
@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;
}https://stackoverflow.com/questions/38092514
复制相似问题