Search code examples
androiddesign-patternsgoogle-play-servicesactivity-recognition

Handling ActivityRecognition.API intents while app is inactive


I'm looking for the general design to implement this:

I want to register to the ActivityRecognition API to receive regular updates in a IntentService called from time to time by this API, while everything else in my App is completely inactive (the goal here is to avoid draining too much battery power on the device).

The issue is that with the new ActivityRecognition API design, it is tied to a GooglePlayServices client, like this for example:

ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
    myGoogleApiClient, 
    3000L, 
    myActivityRecognitionPendingIntent);

And logically, this client disappears if the Activity or Service that created it is destroyed, resulting of the loss of any ActivityRecognition updates.

Obviously everything can go well if I keep a Service (or Activity) alive, holding constantly the GooglePlayServices client used to initiate the ActivityRecognition API (myGoogleApiClient in this example)… But that is what I want to avoid…

How can this be achieved?


Solution

  • You could look at the alarm manager and the tradeoffs there, please see https://developer.android.com/training/scheduling/alarms.html

    If the API auto-wakes up only at the appropriate time, a much more efficient solution is to try a broadcast receiver. If you require the device to remain awake why you are processing, you should look into https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html