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.

1.Add Dependencies


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

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.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