Henry
Henry

Reputation: 167

How to use Google Play Service API Client for Activity Recognition

I want to detect whether I am walking using Google's Play Service Api Client for Activity Recognition. I have read several post on the internet (include stack overflow) about this topic but I still cannot understand how to use the API.

I want to know what is the simplest code to detect whether I am walking or not. And I see this webpage: https://tsicilian.wordpress.com/2013/09/23/android-activity-recognition/

with these code

 * Called when a new activity detection update is available.
 */
@Override
protected void onHandleIntent(Intent intent) {
     //...
      // If the intent contains an update
    if (ActivityRecognitionResult.hasResult(intent)) {
        // Get the update
        ActivityRecognitionResult result = 
          ActivityRecognitionResult.extractResult(intent);

         DetectedActivity mostProbableActivity 
                = result.getMostProbableActivity();

         // Get the confidence % (probability)
        int confidence = mostProbableActivity.getConfidence();

        // Get the type 
        int activityType = mostProbableActivity.getType();
       /* types:
        * DetectedActivity.IN_VEHICLE
        * DetectedActivity.ON_BICYCLE
        * DetectedActivity.ON_FOOT
        * DetectedActivity.STILL
        * DetectedActivity.UNKNOWN
        * DetectedActivity.TILTING
        */
        // process
    }

i just want the result from getMostProbableActivity() but I don't know how to get it. Where should these code be placed, what I need to do besides these codes (for example what do i need to initialise before this, do I need to implement other class? is these code in the mainActivity?)

i can't understand the Google documentation and any tutorial on the web because i Can't comprehend them (maybe it's because they are too high level).

I really appreciate answers to this. Thank you.

Upvotes: 1

Views: 4222

Answers (1)

Kazuki
Kazuki

Reputation: 1492

Actually there is a sample app by Google https://github.com/googlesamples/android-play-location/tree/master/ActivityRecognition

You need to requestActivityUpdates e.g., from your activity

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

googleApiClient.connect();

Intent intent = new Intent(this, YourService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
            googleApiClient,
            1000 /* detection interval */,
            pendingIntent);

Upvotes: 4

Related Questions