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