Affiliate links on Android Authority may earn us a commission.Learn more.
Responding to user activity with the Activity Recognition API
July 25, 2025
Smartphones have become one of those essentials that we carry with useverywhere, so your typical mobile app is going to be used in all kinds of situations and locations.
The more your app knows about this changing context, the better it can adapt to suit the user’scurrentcontext. Whether your app detects the user’s location and displays this information on a map; reverse-geocodes the device’s coordinates into a street address; or uses hardware sensors to respond to changes in light levels or user proximity, there’s a huge range of contextual information that your app can access, and then use to provide a more engaging user experience.

The Activity Recognition API is a unique way of adding contextual awareness to your application, by letting you detect whether the user is currently walking, running, cycling, travelling in a car, or engaged in a range of other physical activities.
This information isessentialfor many fitness applications, but even if you don’t dream of conquering Google Play’s Health & Fitness category, this is still valuable information that you’re able to use in a huge range of applications.

In this article, I’m going to show you how to build an application that uses the Activity Recognition API to detect a range of physical activities, and then display this information to the user.
What is the Activity Recognition API?
The Activity Recognition API is an interface that periodically wakes the device, reads bursts of data from the device’s sensors, and then analyzes this data using powerful machine learning models.
Activity detection isn’t an exact science, so rather than returning a single activity that the user isdefinitelyperforming, the Activity Recognition API returns a list of activities that the usermaybe performing, with a confidence property for each activity. This confidence property is always an integer, ranging from 0 to 100. If an activity is accompanied by a confidence property of 75% or higher, then it’s generally safe to assume that the user is performing this activity, and adjust your application’s behaviour accordingly (although it’s notimpossiblefor multiple activities to have a high confidence percentage, especially activities that are closely related, such as running and walking).

We’re going to display this confidence percentage in our application’s UI, so you’ll be able to seeexactlyhow this property updates, in response to changing user activity.
The Activity Recognition API can detect the following activities:
How can I use the Activity Recognition API?
Google Play’sHealth & Fitnesscategory is packed with apps dedicated to measuring and analyzing your day-to-day physical activities, which makes it a great place to get some inspiration about how you might use Activity Recognition in your own projects. For example, you could use the Activity Recognition API to create an app that motivates the user to get up and stretch when they’ve been stationary for an extended period of time, or an application that tracks the user’s daily run and prints their route on a map, ready for them to post to Facebook (because if Facebook isn’t aware that you got up early and went for a run before work, then did it even really happen?)
While youcoulddeliver the same functionality without the Activity Recognition API, this would require the user to notify your app whenever they’re about to start a relevant activity. You can provide a much better user experience by monitoring these activities, and then performing the desired action automatically.

Although fitness applications are the obvious choice, there’s lots of ways that you can use Activity Recognition in applications thatdon’tfall into the Health & Fitness category. For example, your app might switch to a “hands-free” mode whenever it detects that the user is cycling; request location updates more frequently when the user is walking or running; or display the quickest way to reach a destination by road when the user is travelling in a vehicle.
Create your project
We’re going to build an application that uses the Activity Recognition API to retrieve a list of possible activities and percentages, and then display this information to the user.
The Activity Recognition API requires Google Play Services. To help keep the number of methods in our project under control, I’m only adding the section of this library that’s required to deliver the Activity Recognition functionality. I’m also adding Gson as a dependency, as we’ll be using this library throughout the project:
Next, add the com.google.android.gms.permission.ACTIVITY_RECOGNITION permission to your Manifest:
Create your user interface
Let’s get the easy stuff out of the way and create the layouts we’ll be using throughout this project:
Open the automatically-generated main_activity.xml file, and add the following:
Next, create a detected_activity file:
Open this file and define the layout for each item in our data set:
These layouts reference a few different resources, so open your project’s strings.xml file and define the button’s label, plus all the strings we’ll eventually display in our ListView:
We also need to define a few dimens.xml values. If your project doesn’t already contain a res/values/dimens.xml file, then you’ll need to create one:
Open your dimens.xml file and add the following:
Create your IntentService
Many applications use the Activity Recognition API to monitor activities in the background and then perform an action whenever a certain activity is detected.
Since leaving a service running in the background is a good way to use up precious system resources, the Activity Recognition API delivers its data via an intent, which contains a list of activities the user may be performing at this particular time. By creating a PendingIntent that’s called whenever your app receives this intent, you can monitor the user’s activities without having to create a persistently running service. Your app can then extract the ActivityRecognitionResult from this intent, and convert this data into a more user-friendly string, ready to display in your UI.
Create a new class (I’m using ActivityIntentService) and then implement the service that’ll receive these Activity Recognition updates:
Don’t forget to register the service in your Manifest:
Retrieving Activity Recognition updates
Next, you need to decide how frequently your app should receive new activity recognition data.
Longer update intervals will minimize the impact your application has on the device’s battery, but if you set these intervals too far apart then it could result in your application performing actions based onsignificantlyout-of-date information.
Smaller update intervals mean your application can respond to activity changes more quickly, but it also increases the amount of battery your application consumes. And if a user identifies your application as being a bit of a battery hog, then they may decide to uninstall it.
Note that the Activity Recognition API will attempt to minimize battery use automatically by suspending reporting if it detects that the device has been stationary for an extended period of time, on devices that support the Sensor.TYPE_SIGNIFICANT_MOTION hardware.
Your project’s update interval also affects the amount of data your app has to work with. Frequent detection events will provide more data, which increases your app’s chances of correctly identifying user activity. If further down the line you discover that your app’s activity detection isn’t as accurate as you’d like, then you may want to try reducing this update interval.
Finally, you should be aware that various factors can interfere with your app’s update interval, so there’s no guarantee that your app will receive every single update at thisexactfrequency. Your app may receive updates ahead of schedule if the API has reason to believe the activity state is about to change, for example if the device has just been unplugged from a charger. At the other end of the scale, your app might receive updates after the requested interval if the Activity Recognition API requires additional data in order to make a more accurate assessment.
I’m going to define this update interval (alongside some other functionality) in the MainActivity class:
Displaying the activity data
In this class, we’re going to retrieve the confidence percentage for each activity, by calling getConfidence() on the DetectedActivity instance. We’ll then populate the detected_activity layout with the data retrieved from each DetectedActivity object.
Since each activity’s confidence percentage will change over time, we need to populate our layout at runtime, using an Adapter. This Adapter will retrieve data from the Activity Recognition API, return a TextView for each entry in the data set, and then insert these TextViews into our ListView.
Create a new class, called ActivitiesAdapter, and add the following:
Testing your app
It’s time to put this app to the test! Install your project on an Android device and tap the ‘Track Activity’ button to start receiving activity updates.
Since this data isnevergoing to change while your Android device is sat on your desk, now’s the perfect time to get up and go for a walk (even if itisjust around your house!) Keep in mind that it’s not unusual to see percentages across multiple activities, for example the following screenshot was taken while I was walking.
Although there’s apparently a 2-3% chance that I’m stationary, running, travelling in a vehicle, on a bicycle, or performing some unknown activity, the highest percentage is walking/on foot, so the app has detected the current activity successfully.
Using the Activity Recognition API in real-life projects
In this tutorial we’ve built an application that retrieves activity recognition data and displays a probability percentage for each activity. However, this API returns far more data than most applications actually need, so when you use Activity Recognition in your own projects you’ll typically want to filter this data in some way.
One method, is to retrieve the activity that has the highest probability percentage:
Alternatively, you may want your app to respond to specific activities only, for example requesting location updates more frequently when the user is walking or running. To make sure your app doesn’t perform this actionevery single timethere’s a 1% or higher probability that the user is on foot, you should specify a minimum percentage that this activity must meet, before your application responds:
Wrapping up
In this article, we created an application that uses the Activity Recognition API to monitor user activity, and display this information in a ListView. We also covered some potential ways of filtering this data, ready for you to use in your applications.
Are you going to try using this API in your own projects? Let us know in the comments below!
Thank you for being part of our community. Read ourComment Policybefore posting.