Reputation: 2899
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?
Upvotes: 1
Views: 582
Reputation: 2899
After further testing, and using the updated GooglePlayServices (9.0.2) this looks an impossible task. I solved it by creating a sticky service that stays always in background and does only one thing: maintaining a GoogleApiClient connection alive. Hopefully this won't drain too much power…
Without doing it, killing what was handling the GoogleApiClient was resulting in weird and inconsistent bugs and behaviors depending on the Android version and on the device running it: 30% of the time it was working fine, otherwise the App was crashing, so definately not reliable.
At the end, it looks like the GooglePlayServices are designed this way: we need to keep alive a connection to the GoogleApiClient with a background activity or service, even if this sounds a bit odd, there are maybe some good reasons to act like this.
Upvotes: 1
Reputation: 2525
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
Upvotes: 1