0

Anyone know a great documentation or explanation of the Google ActivityRecognitionAPI? Except this one: https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionApi

This doc is so weak, I would like to know more about this API, and how it's works! And if you have some image of how this API works, I'll be very glad! =)

Thanks!

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
L . Android
  • 33
  • 10
  • All of the Google Play services APIs, Activity Recognition API included, are closed source, so I doubt you'll be able to find much in the way of 'how it works'. – ianhanniballake Feb 20 '16 at 18:19

1 Answers1

3

As of how it works, it's a closed source Google API hence you cannot get the exact answer to this, but most people say that this mostly relies accelerometer sensor, though some also say it uses Gyroscope and some other sensors as well.

As of how to use this, as here's an example code:

1. Adding Play Services dependency

In your build.gradle (module:app), add:

compile 'com.google.android.gms:play-services:10.0.1'

2. Adding permission

In your manifest, add the following permission:

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

3. Connecting the API in onCreate of MainActivity

(Make sure you have declared public GoogleApiClient mApiClient as a member variable)

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

mApiClient.connect();

2. onConnected:

(Paste this in your MainActivity)

@Override
public void onConnected(@Nullable Bundle bundle) {
    Intent intent = new Intent( this, ActivityRecognizedService.class );
    PendingIntent pendingIntent = PendingIntent.getService( this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT );
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates( mApiClient, 3000, pendingIntent );
}

3. ActivityRecognizedService Class Create a new class, ActivityRecognizedService and paste the following code:

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

    public ActivityRecognizedService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (ActivityRecognitionResult.hasResult(intent)) {
            ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
            handleDetectedActivities(result.getProbableActivities());
        }
    }

    private void handleDetectedActivities(List<DetectedActivity> probableActivities) {


     for (DetectedActivity activity : probableActivities) {
            switch (activity.getType()) {
                case DetectedActivity.IN_VEHICLE: {
                    Log.e("ActivityRecogition", "In Vehicle: " + activity.getConfidence());
                    break;
                }
                case DetectedActivity.ON_BICYCLE: {
                    Log.e("ActivityRecogition", "On Bicycle: " + activity.getConfidence());
                    break;
                }
                case DetectedActivity.ON_FOOT: {
                    Log.e("ActivityRecogition", "On Foot: " + activity.getConfidence());
                    break;
                }
                case DetectedActivity.RUNNING: {
                    Log.e("ActivityRecogition", "Running: " + activity.getConfidence());
                    break;
                }



       case DetectedActivity.STILL: {
            Log.e("ActivityRecogition", "Still: " + activity.getConfidence());
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            builder.setContentText("Are you walking?");
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setContentTitle(getString(R.string.app_name));
            NotificationManagerCompat.from(this).notify(0, builder.build());
            break;
        }
        case DetectedActivity.TILTING: {
            Log.e("ActivityRecogition", "Tilting: " + activity.getConfidence());
            break;
        }
        case DetectedActivity.WALKING: {
            Log.e("ActivityRecogition", "Walking: " + activity.getConfidence());
            break;
        }
        case DetectedActivity.UNKNOWN: {
            Log.e("ActivityRecogition", "Unknown: " + activity.getConfidence());
            break;
        }
    }

Through onConnected we call this service, in the above codes we have a callback delay of 3000 milliseconds or 3 seconds, we can vary that. This code generates a toast notification when walking is detected with a confidence greater than 75.

When the service is called, onHandleIntent() is called, which in turn calls handleDetectedActivities().

4) Disconnecting the API Remove callbacks and disconnect API.

if (mApiClient.isConnected()) {
        Intent intent2 = new Intent(context, ActivityRecognizedService.class);
        PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
        ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(mApiClient, pendingIntent);
        mApiClient.disconnect();
    }

Source: How to Recognize User Activity with Activity Recognition

Bugs Buggy
  • 1,514
  • 19
  • 39
  • In section 4 - discdonnecting...I do not understand where this comes from? "AnalysingActivity.this" – Frank Zappa Oct 16 '16 at 02:56
  • @FrankZappa that is your context, the Activity through which you are doing all this. For convenience, I have changed it to MainActivity – Bugs Buggy Oct 18 '16 at 15:27