public class

ActivityRecognitionClient

extends Object
implements GooglePlayServicesClient
java.lang.Object
   ↳ com.google.android.gms.location.ActivityRecognitionClient

Class Overview

The main entry point for activity recognition integration.

The activity recognition service is a low power mechanism that allows application to receive periodic updates of detected user activities. For example, it can detect if the user is currently on foot, in a car, on a bicycle or still.

The activities are detected by periodically waking up the device and reading short bursts of sensor data. It only makes use of low power sensors in order to keep the power usage to a minimum.

To request activity recognition, do the following in a class that implements GooglePlayServicesClient.ConnectionCallbacks and GooglePlayServicesClient.OnConnectionFailedListener:

  // Connect to the ActivityRecognitionService
 ActivityRecognitionClient mActivityRecognitionClient =
         new ActivityRecognitionClient(this, this, this);
 mActivityRecognitionClient.connect();
 
  // Called when a connection to the ActivityRecognitionService has been established.
 public void onConnected(Bundle connectionHint) {
     Intent intent = new Intent(this, MyIntentService.class);
     PendingIntent callbackIntent = PendingIntent.getService(this, 0, intent,
             PendingIntent.FLAG_UPDATE_CURRENT);
     mActivityRecognitionClient.requestActivityUpdates(30000, callbackIntent);
 }
 
To receive activity detections, put the following in a class that extends IntentService:
 protected void onHandleIntent(Intent intent) {
     if (ActivityRecognitionResult.hasResult(intent)) {
         ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
         // Put your application specific logic here (i.e. result.getMostProbableActivity())
     }
 }

Use of the ActivityRecognitionClient requires the com.google.android.gms.permission.ACTIVITY_RECOGNITION permission.

Summary

Public Constructors
ActivityRecognitionClient(Context context, GooglePlayServicesClient.ConnectionCallbacks connectedListener, GooglePlayServicesClient.OnConnectionFailedListener connectionFailedListener)
Public Methods
void connect()
Connects the client to Google Play services.
void disconnect()
Closes the connection to Google Play services.
boolean isConnected()
Checks if the client is currently connected to the service, so that requests to other methods will succeed.
boolean isConnecting()
Checks if the client is attempting to connect to the service.
boolean isConnectionCallbacksRegistered(GooglePlayServicesClient.ConnectionCallbacks listener)
Returns true if the specified listener is currently registered to receive connection events.
boolean isConnectionFailedListenerRegistered(GooglePlayServicesClient.OnConnectionFailedListener listener)
Returns true if the specified listener is currently registered to receive connection failed events.
void registerConnectionCallbacks(GooglePlayServicesClient.ConnectionCallbacks listener)
Registers a listener to receive connection events from this GooglePlayServicesClient.
void registerConnectionFailedListener(GooglePlayServicesClient.OnConnectionFailedListener listener)
Registers a listener to receive connection failed events from this GooglePlayServicesClient.
void removeActivityUpdates(PendingIntent callbackIntent)
Removes all activity updates for the specified PendingIntent.
void requestActivityUpdates(long detectionIntervalMillis, PendingIntent callbackIntent)
Register for activity recognition updates.
void unregisterConnectionCallbacks(GooglePlayServicesClient.ConnectionCallbacks listener)
Removes a connection listener from this GooglePlayServicesClient.
void unregisterConnectionFailedListener(GooglePlayServicesClient.OnConnectionFailedListener listener)
Removes a connection failed listener from the GooglePlayServicesClient.
[Expand]
Inherited Methods
From class java.lang.Object
From interface com.google.android.gms.common.GooglePlayServicesClient

Public Constructors

public ActivityRecognitionClient (Context context, GooglePlayServicesClient.ConnectionCallbacks connectedListener, GooglePlayServicesClient.OnConnectionFailedListener connectionFailedListener)

Parameters
context The context to use for the connection.
connectedListener The callbacks invoked when the client is connected.
connectionFailedListener The listener which will be notified if the connection attempt fails.

Public Methods

public void connect ()

Connects the client to Google Play services. This method returns immediately, and connects to the service in the background. If the connection is successful, onConnected(Bundle) is called. On a failure, onConnectionFailed(ConnectionResult) is called.

public void disconnect ()

Closes the connection to Google Play services. No calls can be made on this object after calling this method.

public boolean isConnected ()

Checks if the client is currently connected to the service, so that requests to other methods will succeed. Applications should guard client actions caused by the user with a call to this method.

Returns
  • true if the client is connected to the service.

public boolean isConnecting ()

Checks if the client is attempting to connect to the service.

Returns
  • true if the client is attempting to connect to the service.

public boolean isConnectionCallbacksRegistered (GooglePlayServicesClient.ConnectionCallbacks listener)

Returns true if the specified listener is currently registered to receive connection events.

Parameters
listener The listener to check for.
Returns
  • true if the specified listener is currently registered to receive connection events.

public boolean isConnectionFailedListenerRegistered (GooglePlayServicesClient.OnConnectionFailedListener listener)

Returns true if the specified listener is currently registered to receive connection failed events.

Parameters
listener The listener to check for.
Returns
  • true if the specified listener is currently registered to receive connection failed events.

public void registerConnectionCallbacks (GooglePlayServicesClient.ConnectionCallbacks listener)

Registers a listener to receive connection events from this GooglePlayServicesClient. If the service is already connected, the listener's onConnected(Bundle) method will be called immediately. Applications should balance calls to this method with calls to unregisterConnectionCallbacks(ConnectionCallbacks) to avoid leaking resources.

If the specified listener is already registered to receive connection events, this method will not add a duplicate entry for the same listener, but will still call the listener's onConnected(Bundle) method if currently connected.

Note that the order of messages received here may not be stable, so clients should not rely on the order that multiple listeners receive events in.

Parameters
listener the listener where the results of the asynchronous connect() call are delivered.

public void registerConnectionFailedListener (GooglePlayServicesClient.OnConnectionFailedListener listener)

Registers a listener to receive connection failed events from this GooglePlayServicesClient. Unlike registerConnectionCallbacks(GooglePlayServicesClient.ConnectionCallbacks), if the service is not already connected, the listener's onConnectionFailed(ConnectionResult) method will not be called immediately. Applications should balance calls to this method with calls to unregisterConnectionFailedListener(OnConnectionFailedListener) to avoid leaking resources.

If the specified listener is already registered to receive connection failed events, this method will not add a duplicate entry for the same listener.

Note that the order of messages received here may not be stable, so clients should not rely on the order that multiple listeners receive events in.

Parameters
listener the listener where the results of the asynchronous connect() call are delivered.

public void removeActivityUpdates (PendingIntent callbackIntent)

Removes all activity updates for the specified PendingIntent.

Calling this function requires the com.google.android.gms.permission.ACTIVITY_RECOGNITION permission.

Parameters
callbackIntent pending intent object that no longer needs activity detection updates
Throws
IllegalStateException if this method was called at an inappropriate time, such as before the LocationServiceClient has bound to the remote service.

public void requestActivityUpdates (long detectionIntervalMillis, PendingIntent callbackIntent)

Register for activity recognition updates.

The activities are detected by periodically waking up the device and reading short bursts of sensor data. It only makes use of low power sensors in order to keep the power usage to a minimum. For example, it can detect if the user is currently on foot, in a car, on a bicycle or still. See DetectedActivity for more details.

The activity detection update interval can be controlled with the detectionIntervalMillis parameter. Larger values will result in fewer activity detections while improving battery life. Smaller values will result in more frequent activity detections but will consume more power since the device must be woken up more frequently.

Activities may be received more frequently than the detectionIntervalMillis parameter if another application has also requested activity updates at a faster rate. It may also receive updates faster when the activity detection service receives a signal that the current activity may change, such as if the device has been still for a long period of time and is then unplugged from a phone charger.

Activities may arrive several seconds after the requested detectionIntervalMillis if the activity detection service requires more samples to make a more accurate prediction.

To conserve battery, activity reporting may stop when the device is 'STILL' for an extended period of time. It will resume once the device moves again. This only happens on devices that support the Sensor.TYPE_SIGNIFICANT_MOTION hardware.

A common use case is that an application wants to monitor activities in the background and perform an action when a specific activity is detected. To do this without needing a service that is always on in the background consuming resources, detected activities are delivered via an intent. The application specifies a PendingIntent callback (typically an IntentService) which will be called when activities are detected. See the documentation of PendingIntent for more details.

Applications can call this function several times in a row with the same callbackIntent to change the desired activity detection interval.

Calling this function requires the com.google.android.gms.permission.ACTIVITY_RECOGNITION permission.

Parameters
detectionIntervalMillis the desired time between activity detections. Larger values will result in fewer activity detections while improving battery life. A value of 0 will result in activity detections at the fastest possible rate.
callbackIntent a PendingIntent to be sent for each activity detection.
Throws
IllegalStateException if this method was called at an inappropriate time, such as before the LocationServiceClient has bound to the remote service.

public void unregisterConnectionCallbacks (GooglePlayServicesClient.ConnectionCallbacks listener)

Removes a connection listener from this GooglePlayServicesClient. Note that removing a listener does not generate any callbacks.

If the specified listener is not currently registered to receive connection events, this method will have no effect.

Parameters
listener the listener to unregister.

public void unregisterConnectionFailedListener (GooglePlayServicesClient.OnConnectionFailedListener listener)

Removes a connection failed listener from the GooglePlayServicesClient. Note that removing a listener does not generate any callbacks.

If the specified listener is not currently registered to receive connection failed events, this method will have no effect.

Parameters
listener the listener to unregister.