Detect user’s activity using Activity Recognition Transition API

Understanding what people are doing can help your app better adapt your user’s needs.Transition API can tell if your users are walking, running, cycling, or in a vehicle and you can use it to improve your app experience.Transition API combine various signals like location and sensor data to determine when the user has started or ended an activity like walking or driving.

Since November 2017, the Transition API use to power the Driving Do-Not-Disturb feature launched on the Pixel 2.Now, Activity Recognition Transition API available to all Android developers.Transition API take care of stillness means the user parked their car and ended a drive or simply stopped at a traffic light and will continue on.

Use Case


If a user starts running, you’ll get a callback indicating the most probable activity is running.If the confidence is 75 or higher, you know you can act on it and show notification asking if they want to start tracking their run.

When an app is in use and the user starts driving, you can offer popup switch to car mode dialogue.

You can also use it for historical cases such as showing your users when they parked their car or how long they spent commuting to work each day.

Mileage tracking app could start tracking miles when a user starts driving, or a messaging app could mute all conversations until the user stops driving.

1.Add Dependencies


To declare a dependency to the API, add a reference to the Google maven repository and add an implementation entry to com.google.android.gms:play-services-location:12.0.0 to the dependencies section of your app build.gradle file.

2.Add Permission


Specify the com.google.android.gms.permission.ACTIVITY_RECOGNITION permission in the app manifest.

3.Register for activity transition updates


To start receiving notifications about activity transitions, you must implement, An ActivityTransitionRequest object that specifies the type of activity and transition.PendingIntent callback where your app receives notifications.

ActivityTransitionRequest

You must create a list of ActivityTransition objects, which represent the transition that you want to receive notifications about. An ActivityTransition object includes the 1.An activity type  IN_VEHICLE,ON_BICYCLE,RUNNING,STILL,WALKING

2.A transition type of ACTIVITY_TRANSITION_ENTER or ACTIVITY_TRANSITION_EXIT. For more information, refer to theActivityTransition class.

The following code shows how to create a list of ActivityTransition objects:

PendingIntent

Reducing memory consumption is important to keep the phone running smoothly.When using the API, a common use case is that an application wants to monitor activity in the background and perform an action when a specific activity is detected.Typically, when developing for Android, you don’t use a persistent running service to handle this.That consumed a lot of resources.This API removes that burden by delivering the data via an intent.The application specifies a pending intent callback typically an intent service, which will be called with an intent when activities are detected.So there’s no need to keep the service always running in the background.

After successfully registering for activity transition updates, your app receives notifications in the registered PendingIntent.

4.Receive activity transition events


When the requested activity transition occurs, you app receives an Intent callback.The events are ordered in chronological order, for example, if an app requests for the IN_VEHICLE activity type on the ACTIVITY_TRANSITION_ENTER and ACTIVITY_TRANSITION_EXIT transitions, then it receives an ActivityTransitionEvent object when the user starts driving, and another one when the user transitions to any other activity.

Your application will receive callbacks with extras containing an activity recognition result.This contains a list of activities that the user may be doing at the particular time.

5.Deregister for activity transition updates


Deregister for activity transition updates by calling the removeActivityTransitionUpdates() method of the ActivityRecognitionClient and passing your PendingIntent object as a parameter.

 

Download this project from GitHub

 

 

Leave a Reply

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