我正在尝试为我正在处理的跟踪器的循环事件注册 Fence API,用于自动暂停/恢复功能。DetectedActivityFence.stopping
然而,广播被捕获DetectedActivityFence.starting
并且DetectedActivityFence.during
没有到达广播接收器,因此自动暂停正在工作,但不幸的是恢复没有。我错过了什么吗?
注意:我尝试使用HeadphoneFence.pluggingIn
并且HeadphoneFence.unplugging
能够在同一个广播接收器中获取这两个事件的栅栏。
将栅栏注册为;
AwarenessFence starting = DetectedActivityFence.starting(DetectedActivityFence.ON_BICYCLE);
AwarenessFence during = DetectedActivityFence.during(DetectedActivityFence.ON_BICYCLE);
AwarenessFence stopping = DetectedActivityFence.stopping(DetectedActivityFence.ON_BICYCLE);
final FenceUpdateRequest.Builder fenceUpdateBuilder = new FenceUpdateRequest.Builder();
for (AwarenessFence awarenessFence : fenceList) {
fenceUpdateBuilder.addFence(fenceKey, awarenessFence, mPendingIntent);
}
googleApiClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
Awareness.FenceApi.updateFences(
googleApiClient, fenceUpdateBuilder.build())
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
googleApiClient.disconnect();
}
});
}
@Override
public void onConnectionSuspended(int i) {
}
});
我的广播
@Override
public void onReceive(Context context, Intent intent) {
FenceState fenceState = FenceState.extract(intent);
if (TextUtils.equals(fenceState.getFenceKey(), FENCE_CYCLING_ACTIVITY)) {
switch (fenceState.getCurrentState()) {
case FenceState.TRUE:
Intent serviceIntent = new Intent(context, ActivityRecordService.class);
serviceIntent.putExtra(ActivityRecordService.EXTRA_AWARENESS_INPUT, ActivityRecordService.AWARENESS_STARTED_CYCLING);
context.startService(serviceIntent);
break;
case FenceState.FALSE:
Intent serviceIntent2 = new Intent(context, ActivityRecordService.class);
serviceIntent2.putExtra(ActivityRecordService.EXTRA_AWARENESS_INPUT, ActivityRecordService.AWARENESS_STOPPED_CYCLING);
context.startService(serviceIntent2);
break;
}
}
}