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