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

29 / Mar / 2015 by Himanshu Kumar 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.

[xml]
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
[/xml]

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

[java]
GoogleApiClient.ConnectionCallbacks connectionCallbacks = new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
}

@Override
public void onConnectionSuspended(int i) {
}
};
[/java]

[java]
GoogleApiClient.OnConnectionFailedListener onConnectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}
};
[/java]

Step 4: Connect the GoogleApiClient with ActivityRecognition.API.

[java]
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
.addApi(ActivityRecognition.API)
.addConnectionCallbacks(this) //this is refer to connectionCallbacks interface implementation.
.addOnConnectionFailedListener(this) //this is refer to onConnectionFailedListener interface implementation.
.build();

googleApiClient.connect();

[/java]

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

[java]
public class ActivityRecognizationIntentService extends IntentService {

public ActivityRecognizationIntentService() {
super("ActivityRecognizationIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
if(ActivityRecognitionResult.hasResult(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 "";
}
}
[/java]

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

[java]
//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);

ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(googleApiClient,1,pendingIntent);
[/java]

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

FOUND THIS USEFUL? SHARE IT

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….
    Thanks

    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 *