Google's Activity Recognition Transition API

Identify transition in the user's activity

Manas Malla

May 20 2020

4 min read



Humans are the most radical creatures on Earth. Users have their phones with them throughout the day as they drive, walk, exercise, work, and play. But until now, it's been hard for apps to adjust their experience to a user's continually changing environment and activity. Understanding what users are doing in the physical world allows your app to be smarter about how to interact with them.

For example, a messaging app can ask, "tell me when the user has entered or exited a vehicle", to set the user's status as busy. Similarly, a parking detection app can ask," tell me when the user has exited a vehicle and started walking", to save the user's parking location or, an app could start to track the distance travelled by the user when he/she starts to walk to optimize the battery consumption. Additionally, another app can switch to car mode when it detects that the user has started driving.

To do this in the past, developers spent valuable engineering time combining various signals (location,sensor, etc.) to determine when an activity like walking or driving had started or ended. Even worse, when apps are independently and continuously checking for changes in user activity, battery life suffers.

I too made an app using Activity Recognition Transition API in my app, EcoFun, which promotes people to be ecofriendly by walking, running, cycling or commuting by public transport.

Be EcoFriendly! Play More! Win More! Nourish our Blue & Green Planet More 🌱 !

The API... in a nutshell The Activity Recognition Transition API is easy to implement and work with. We primarily recieve two pieces of informaition i.e, Activity and Transition, from the Google Play services when a user starts to interact with the real-world by performing one of the following different supported activity types:

- Walking
- Running

For example from driving to walking then we recieve an updates with values: Activity: Driving; Transition: Exit; Activity: Walking; Transition: Enter

The API provides this data as a Background Service and and Intent Service and data can be broadcasted to MainActivity using LocalBroadcastManager. My app handles these intents to start calculating distance using Location API to finally give the user his ecoScore by multiplying the distance he travelled with the coefficient of carbon footprint per kilometre for the journey.

Ok, enough of intro, let’s dig in and get started to implement the Google Activity Recognition Transition API. First off, add this into you build.gradle (Module: app) file :

implementation '' Then open your manifest and this following line just below the Then open your file or the Activity Java file where you want to implement the API and then check for permissions either in your onCreate or in an onClick method to be implemented later : Then add this line : private static final String TAG = MainActivity.class.getSimpleName(); Now go to activity_main and design the layout you require, here we will be creating a button having title ‘Start Tracking’ and 2 textviews with text ‘Activity:’ and ‘Transition:’ respectively. You can gather your own assets or download the res folder from here: Download Assets Here is my activity_main.xml for reference: Now time to write some code, so now create a class named Constants, and type the following in the class: public static final String BROADCAST_DETECTED_ACTIVITY = "activity_intent"; //The key which we will use to check if the intent is the one we require static final long DETECTION_INTERVAL_IN_MILLISECONDS = 30 * 1000; //The time for which every milliseconds the app checks for updates public static final int CONFIDENCE = 70; // The confidence value which indicates the accuracy of the activity transition. Create a class named and extend it from IntentService and implement the defaut methods. We implement our method to get the activity in the form of a string from DetectedActivity variable type and also broadcast the data to MainActivity. Now create the methods and variables so as to make look like this : We need to create another background service class named which runs in background and triggers the activities in an interval basis. ….To BE Continued!

© 2023 Manas Malla

Archive Privacy Policy Home