Android Activity Recognition

Activity Recognition gives our Android device the ability to detect a number of our physical activities like walking, riding a bicycle, driving a car or standing idle. All that by simply using an API to access Google Play Services, an increasingly crucial piece of software available to all Android versions.

As in the article on geofencing, we will download the sample app ( at the Android developer’s site and start playing with it, eventually modifying parts of it to fit our purposes. We will show here only the most relevant code sections.

The first thing to note is that we need a specific permission to use Activity Recognition:

<!-- Inside manifest file -->

As with geofencing or location updates, we use the API to request Google Play Services to analyse our data and provide us with the results. The chain of method calls for requesting updates is similar to that of geofencing:

  1. Make sure that Google Play Services is available
  2. As an Activity Recognition client, request a connection
  3. Once connected, Location Services calls back the onConnected() method in our app
  4. Proceed with the updates request via a Pending Intent pointing to an IntentService we have written
  5. Google Location Services sends out its activity recognition updates as Intent objects, using the PendingIntent we provided. Get & process the updates in our IntentService’s onHandleIntent() method.

The sample app writes all the updates in a log file, and that is OK if we like that sort of thing…though a closer look at the data makes us realize that most of it is garbage. Do we really need to know that we have a 27% chance of being driving a vehicle and a 7% chance of riding a bicycle when we are in fact sitting idle at our desk? Not really. What we want is the most significant data, and in this case, that would be the most probable activity:


 * Service that receives ActivityRecognition updates. It receives updates
 * in the background, even if the main Activity is not visible.
public class ActivityRecognitionIntentService extends IntentService {  
     * Called when a new activity detection update is available.
    protected void onHandleIntent(Intent intent) {
          // If the intent contains an update
        if (ActivityRecognitionResult.hasResult(intent)) {
            // Get the update
            ActivityRecognitionResult result = 

             DetectedActivity mostProbableActivity 
                    = result.getMostProbableActivity();

             // Get the confidence % (probability)
            int confidence = mostProbableActivity.getConfidence();

            // Get the type 
            int activityType = mostProbableActivity.getType();
           /* types:
            * DetectedActivity.IN_VEHICLE
            * DetectedActivity.ON_BICYCLE
            * DetectedActivity.ON_FOOT
            * DetectedActivity.STILL
            * DetectedActivity.UNKNOWN
            * DetectedActivity.TILTING
            // process

Instead of writing the updates to a log file, it is simpler to just store them in memory (e.g. in a static List in a dedicated class) and display them to the user of our app. One way to do this would be by using a Fragment to display the updates on top of a Google map. As commented in previous articles, Fragments were introduced in Honeycomb but are also available to older Android versions through the Support Library.

<?xml version="1.0" encoding="utf-8"?>


<!-- activity recognition -->


Once we define our own XML layout for the ActReconFragment and give it a transparent background (left to the reader as an exercise), we will get a nice overlaid display like this:


Since we have chosen to show the most probable activity to the users of our app, we need the display to be dynamic, like a live feed. For that, we can add a local broadcast in our service:

//inside ActivityRecognitionIntentService 's onHandleIntent
Intent broadcastIntent = new Intent();

// Give it the category for all intents sent by the Intent Service
// Set the action and content for the broadcast intent

// Broadcast *locally* to other components in this app

We are using a LocalBroadcastManager (included in android 3.0 and above and in the support library v4 for early releases). Apart from providing our own layout to position the Activity Detection panel on top of a map, the only new code snippet we wrote is the above local broadcast. For the remainder below, we have simply re-positioned the sample app’s code in a fragment and use in-memory storage of the activity updates instead of using a log file. The receiver on that local broadcast is in our Fragment:

public class ActReconFragment extends Fragment{

 // Intent filter for incoming broadcasts from the IntentService
 IntentFilter mBroadcastFilter;

 // Instance of a local broadcast manager
 private LocalBroadcastManager mBroadcastManager;

 * Called when the corresponding Map Activity's
 * onCreate() method has completed.
public void onActivityCreated(Bundle savedInstanceState) {

  // Set the broadcast receiver intent filer
  mBroadcastManager = LocalBroadcastManager.getInstance(getActivity());

  // Create a new Intent filter for the broadcast receiver
  mBroadcastFilter = new IntentFilter(ActivityUtils.ACTION_REFRESH_STATUS_LIST);


* Broadcast receiver that receives activity update intents
* This receiver is local only. It can't read broadcast Intents from other apps.
BroadcastReceiver updateListReceiver = new BroadcastReceiver() {
   public void onReceive(Context context, Intent intent) {
     // When an Intent is received from the update listener IntentService, 
     // update the display.

Live feed shots:

Once we have taken care of the display, we need to move on to other important aspects like what to do with those activity updates. The sample app gives us one example of that in the ActivityRecognitionIntentService:

   // If the current type is "moving" i.e on foot, bicycle or vehicle
  // The activity has changed from the previous activity
  // The confidence level for the current activity is >= 50%
  && (confidence >= 50)) {

  // do something useful

Simply getting the most probable activity might be OK for displaying purposes, but might not be enough for an app to act on it and do something useful. We need to make sure that the type of activity and the corresponding confidence level (i.e. probability) are adequate for our purposes. While a detected activity type of “Unknown” with a confidence level of 52% is next to useless, knowing that the user is moving in a vehicle as opposed to walking can be put to good use: increase the frequency of location updates, enlarge the map area of available points of interest, etc…

Activity Recognition has been added as an experimental feature to this Geofencing app. Check it out and feel free to post any feedback.

Reference: Android Activity Recognition from our JCG partner Tony Sicilian at the Tony’s Blog blog.
Related Whitepaper:

Rapid Android Development: Build Rich, Sensor-Based Applications with Processing

Create mobile apps for Android phones and tablets faster and more easily than you ever imagined

Use 'Processing', the free, award-winning, graphics-savvy language and development environment, to work with the touchscreens, hardware sensors, cameras, network transceivers, and other devices and software in the latest Android phones and tablets.

Get it Now!  

One Response to "Android Activity Recognition"

  1. Anber says:

    thank you very much, that was good example

    also I think it will be nice to have the source code for the projects in this cool site :)

Leave a Reply

nine × 5 =

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: