Activity Recognition using new GoogleApiClient not firing Activity Updates

1.5k Views Asked by At

I am trying to set up ActivityRecognition in my app using the new GoogleApiClient. I couldn't find any documentation anywhere (Official android docs still refer to the now deprecated ActivityRecognitionClient).

The code that I managed to write is not triggering Activity Updates.

1) Inside my MainActivity's onCreate :

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(ActivityRecognition.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

mGoogleApiClient.connect();

2) Inside onConnected :

    Intent i = new Intent(this, ActivityRecognitionIntentService.class);
    PendingIntent mActivityRecognitionPendingIntent = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

    Log.e("Aditya", "Connected to ActRec");
    ActivityRecognitionApi actRecAPI = new ActivityRecognitionApi()
    {
        public PendingResult<Status> requestActivityUpdates(GoogleApiClient googleApiClient, long l, PendingIntent pendingIntent)
        {
            Log.e("Aditya", "inside requestActivityUpdates()");
            return null;
        }

        public PendingResult<Status> removeActivityUpdates(GoogleApiClient googleApiClient, PendingIntent pendingIntent)
        {
            return null;
        }
    };

    actRecAPI.requestActivityUpdates(mGoogleApiClient, 0, mActivityRecognitionPendingIntent);

My ActivityRecognitionIntentService never receives an update.

What am I doing wrong? Or is there any sort of documentation on ActivityRecognition through GoogleApiClient anywhere?

1

There are 1 best solutions below

1
On BEST ANSWER

Solved it. After connecting to GoogleApiClient I just had to do this to start receiving Activity Updates.

ActivityRecognition.ActivityRecognitionApi.
  requestActivityUpdates(mGoogleApiClient, 0, mActivityRecognitionPendingIntent);

No need to create your own ActivityRecognitionApi object.