Activity Recognition using new GoogleApiClient [ActivityRecognitionClient is deprecated in december 2014]

29 / Mar / 2015 by Himanshu Singh 2 comments

As per Google play services 6.5 Highlights : The ActivityRecognitionClientLocationClient, and PlusClientclasses are deprecated. If you used those APIs in your app and want to call Google Play services 6.5 or higher APIs, you must switch to the new programming model that utilizes GoogleApiClient.

As per docs, GoogleApiClient is able to add the multiple APIs  into the single client.
i.e.  – In one GoogleApiClient, You can add both  LocationServices.API and ActivityRecognition.API, or different API client.

What is mean by Google Activity Recognition?

For the Android world, It comes with a lot of new ideas for the application. Activity Recognition API recognize various activities like :

  • The device is in a vehicle, such as a car.
  • The device is on a bicycle.
  • The device is on a user who is walking or running.
  • The device is still (not moving).
  • The device angle relative to gravity changed significantly and so on.

How well does it work?

In my personal testing, I get following thing. When i am walking from step-down to vehicle, there is some delay (approximate 2-4 second).

Useful Methods
  1. ActivityRecognitionClient
    • connect()
    • disconnect()
    • requestActivityUpdates()
    • removeActivityUpdates()
  2. ActivityRecognitionResult
    • hasResult()
    • extractResult()
    • getMostProbableActivity().getType()
    • getMostProbableActivity().getConfidence()

Step to Implement

Step 1: Add Google Play Service(6.5 or higher) library  in your project.

Step 2: Add required permission in the Manifest.

<uses-permission android:name="" />

Step 3: Implement GoogleApiClient.ConnectionCallbacks and GoogleApiClient.OnConnectionFailedListener interface.

GoogleApiClient.ConnectionCallbacks connectionCallbacks = new GoogleApiClient.ConnectionCallbacks() {
public void onConnected(Bundle bundle) {

public void onConnectionSuspended(int i) {

GoogleApiClient.OnConnectionFailedListener onConnectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {
public void onConnectionFailed(ConnectionResult connectionResult) {


Step 4: Connect the GoogleApiClient with ActivityRecognition.API.

GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this) //this is refer to connectionCallbacks interface implementation.
.addOnConnectionFailedListener(this) //this is refer to onConnectionFailedListener interface implementation.



Step 5: Create IntentService and override onHandleIntent(Intent intent) method. Don’t forget to add this to your manifest file.

public class ActivityRecognizationIntentService extends IntentService {

public ActivityRecognizationIntentService() {

protected void onHandleIntent(Intent intent) {
ActivityRecognitionResult activityRecognitionResult = ActivityRecognitionResult.extractResult(intent);
DetectedActivity detectedActivity = activityRecognitionResult.getMostProbableActivity();

int confidence = detectedActivity.getConfidence();
String recognizeActivity = getActivityName(detectedActivity.getType());

Log.d("Himanshu","Confidence : " + confidence);
Log.d("Himanshu","RecognizeActivity : " + recognizeActivity);


private String getActivityName(int activityType){
switch (activityType){
case DetectedActivity.IN_VEHICLE:
return "IN_VEHICLE";
case DetectedActivity.ON_BICYCLE:
return "ON_BICYCLE";
case DetectedActivity.STILL:
return "STILL";
case DetectedActivity.TILTING:
return "TILTING";
case DetectedActivity.RUNNING:
return "RUNNING";
case DetectedActivity.ON_FOOT:
return "ON_FOOT";
case DetectedActivity.WALKING:
return "WALKING";
case DetectedActivity.UNKNOWN:
return "UNKNOWN";
return "";

Step 6: Create a pending intent and pass it in ActivityRecognitionApi.requestActivityUpdates() as a arguments.

//Before request API request, GoogleApiClient must be in connected mode.
Intent intent = new Intent(this,ActivityRecognizationIntentService.class);
PendingIntent pendingIntent = PendingIntent.getService(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);


Now, I feel really excited when I think about use-case of GoogleApiClient APIs in Android world.


comments (2)

  1. Ravindra

    Thanks for the article, but I want to know that in ActivityTransition API, sometimes doesn’t return any result(i.e successfully registered with google client but doesn’t return any result) in some device like Motorola: OS- 5.0. Motorola: OS – 7.0.So can you please explain the issue and have you face the same?
    Waiting for your reply….

  2. Pingback: Which API is really depreciated ActivityRecognitionApi or ActivityRecognitionClient – program faq

Leave a Reply

Your email address will not be published. Required fields are marked *