Android. How to use ActivityRecognitionApi library from Google.

Published by Igor Khrupin on

Case:
We need know current user activity.
Using ActivityRecognitionApi we can get if user driving, walking, running, ride bicycle.

Android get information from all the sensors and analyse it using ActivityRecognitionApi.

Below simple sample how to implement it.

Please follow the steps:

1. Create android project in Android Studio.
2. Add compile ‘com.google.android.gms:play-services-location:8.4.0’ dependency in build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.hrupin.activityrecognitionwithgoogle"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.google.android.gms:play-services-location:8.4.0'
}

3. Modify AndroidManifest.xml and add
“com.google.android.gms.permission.ACTIVITY_RECOGNITION” uses-permission

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hrupin.activityrecognitionwithgoogle">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
        <service android:name=".ActivityRecognitionService"></service>

        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

4. Add ActivityRecognitionService which will handle the results.

package com.hrupin.activityrecognitionwithgoogle;

import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class ActivityRecognitionService extends IntentService	 {

    public static final String ACTION = "com.hrupin.activityrecognitionwithgoogle.ACTIVITY_RECOGNITION_DATA";
    private String TAG = this.getClass().getSimpleName();
	public ActivityRecognitionService() {
		super("My Activity Recognition Service");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		if(ActivityRecognitionResult.hasResult(intent)){
			ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
			Log.i(TAG, getType(result.getMostProbableActivity().getType()) +"\t" + result.getMostProbableActivity().getConfidence());
			Intent i = new Intent(ACTION);
			i.putExtra("Activity", getType(result.getMostProbableActivity().getType()) );
			i.putExtra("Confidence", result.getMostProbableActivity().getConfidence());
			sendBroadcast(i);
		}
	}
	
	private String getType(int type){
	    if(type == DetectedActivity.IN_VEHICLE) {
            return "In Vehicle";
        }else if(type == DetectedActivity.ON_BICYCLE) {
            return "On Bicycle";
        }else if(type == DetectedActivity.ON_FOOT) {
            return "On Foot";
        }else if(type == DetectedActivity.RUNNING) {
            return "Running";
        }else if(type == DetectedActivity.STILL) {
            return "Still";
        }else if(type == DetectedActivity.TILTING) {
            return "Tilting";
        }else if(type == DetectedActivity.UNKNOWN) {
            return "Unknown";
        }else if(type == DetectedActivity.WALKING) {
            return "Walking";
        }else {
            return "...";
        }
	}

}

6. Added Activity for visualisation.

package com.hrupin.activityrecognitionwithgoogle;

import android.support.v7.app.AppCompatActivity;
import java.io.File;
import java.io.FileWriter;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.format.Time;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.ActivityRecognition;

public class MainActivity extends AppCompatActivity implements ConnectionCallbacks, OnConnectionFailedListener,
        OnClickListener {

    private static final String version = "1";
    private PendingIntent pIntent;
    private BroadcastReceiver receiver;
    private TextView tvActivity;
    private GoogleApiClient client;
    private Button bSend;
    private Button bClear;
    private File tempFile;
    private FileWriter writer;

    private static String curAct;
    private static int curCon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvActivity = (TextView) findViewById(R.id.tvActivity);
        bSend = (Button) findViewById(R.id.buttonSend);
        bSend.setOnClickListener(this);
        bClear = (Button) findViewById(R.id.buttonClear);
        bClear.setOnClickListener(this);

        int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (resp == ConnectionResult.SUCCESS) {
            client = new GoogleApiClient.Builder(this).addApi(ActivityRecognition.API).addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this).build();
            client.connect();
        } else {
            Toast.makeText(this, "Please install Google Play Service.", Toast.LENGTH_SHORT).show();
        }

        receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                String time = getTime();
                String act = intent.getStringExtra("Activity");
                int con = intent.getExtras().getInt("Confidence");
                Toast.makeText(MainActivity.this, time + ", A: " + act + ", C: " + con, Toast.LENGTH_SHORT).show();
                if(act != null){
                    if(curAct == null || (!curAct.equals(act) || curCon != con)){
                        curAct = act;
                        curCon = con;
                        String v = time + ": Activity :" + act + " " + "Confidence : "
                                + con + "\n";
                        v += tvActivity.getText();
                        tvActivity.setText(v);
                    }
                }
            }
        };

        IntentFilter filter = new IntentFilter();
        filter.addAction(ActivityRecognitionService.ACTION);
        registerReceiver(receiver, filter);
        Toast.makeText(this, "STARTED !!!", Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (client != null) {
            ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(client, pIntent);
            client.disconnect();
            clear();
            Toast.makeText(this, "STOPPED !!!", Toast.LENGTH_LONG).show();
        }
        unregisterReceiver(receiver);
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        Toast.makeText(this, "Connection Failed", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnected(Bundle arg0) {
        if (client != null) {
            Intent intent = new Intent(this, ActivityRecognitionService.class);
            pIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(client, 1000, pIntent);
        }
    }

    @Override
    public void onConnectionSuspended(int cause) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.buttonSend) {
            String text = null;
            try {
                text = tvActivity.getText().toString();
            } catch (Exception e) {

            }
            if (text != null) {
                try {
                    tempFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                            "report" + version + ".txt");

                    writer = new FileWriter(tempFile);
                    writer.write(text);
                    writer.close();
                    Toast.makeText(this, "Temporarily saved contents in " + tempFile.getPath(), Toast.LENGTH_SHORT)
                            .show();
                } catch (Exception e) {

                }

                tempFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                        "report" + version + ".txt");

                Intent email = new Intent(Intent.ACTION_SEND);
                email.putExtra(android.content.Intent.EXTRA_SUBJECT, "[com.hrupin.activityrecognitionwithgoogle] version " + version);

                email.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + tempFile.getAbsoluteFile()));
                email.setType("message/rfc822");
                startActivity(Intent.createChooser(email, "Send Report"));
                clear();
            }
        } else if (v.getId() == R.id.buttonClear) {
            clear();
        }

    }

    private void clear() {
        try {
            tvActivity.setText(getTime() + ":: cleared");
        } catch (Exception e) {

        }
        curAct = null;
        curCon = 0;
    }

    private String getTime() {
        Time t = new Time();
        t.setToNow();
        return t.format2445();
    }

}

Full source code here:

Download it from github

0 Comments

Leave a Reply

Avatar placeholder

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.