【发布时间】: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