I am trying to use Google Fit API and I succeeded with Google's sample app.When I ran it, it popped a sign in window and I chose my account then got to use the API as expected.
In my app, I must maintain constant access to the API and therefore I initiate my google client in the service and trying to make requests to the Google Fit data from service. The initialization looks like this:
public class MyService extends Service {
....
....
....
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient
= new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
// automanage can not be used in Service, so it is disabled.
// .enableAutoManage(
//
// /* FragmentActivity */
// new FragmentActivity(),
// this /* OnConnectionFailedListener */)
.addApi(ActivityRecognition.API)
.addApi(Fitness.RECORDING_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
.addApi(Fitness.HISTORY_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.setAccountName("myemail@gmail.com")
.build();
}
.....
.....
}
And when I do this I get the following error:
I/fitapi: Google Play services connection failed. Cause: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{1887aff: android.os.BinderProxy@1cd040cc}, message=null}
Clearly the .setAccountName(String) didn't work as implied by the error message.
My question is as follows: how do I sign on from service if .setAccountName is not working?
Doing the .enableAutoManage() from the main activity works but it doesn't allow for the background processes to continue fitness data collection. Obviously .enableAutoManage() is not going to work in the service because service doesn't run on the UI thread.
Any help would be appreciated.Thanks.