0

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.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
372
  • 237
  • 2
  • 10
  • Kindly check this [thread](http://stackoverflow.com/questions/23736137/onconnectionfailed-geving-sign-in-required) for some pointers. Seems you have similar issue. – ReyAnthonyRenacia May 28 '16 at 15:04
  • this thread doesn't talk about how to implement play services as part of Service. – 372 Jun 02 '16 at 02:36

1 Answers1

0

I figured it out. The problem was with my package name not registered on Google API console. Very common mistake that is not trivial to debug. The moral of the story? Read the documentation and the official guidelines very carefully.

372
  • 237
  • 2
  • 10