Awareness Fence API

381 Views Asked by At

I am trying to register Fence API for cycling events for the tracker I am working on, for auto-pause/resume feature. The broadcast catches DetectedActivityFence.stopping however DetectedActivityFence.starting and DetectedActivityFence.during are not coming to broadcast receiver so the auto pause is working but the resume is not unfortunately. Am I missing something?

NOTE: I tried fences with HeadphoneFence.pluggingIn and HeadphoneFence.unplugging and able to get both events in same broadcast receiver.

Registering fences as;

    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) {

            }
        });

My broadcast

 @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;
            }
        }
    }
0

There are 0 best solutions below