Handle GoogleFit in the background in the Android app

I am trying to connect my application to Google Fit. I am using IntentService, which should do the following. It turns out when I have information about the steps. At this point, I am trying to create a GoogleApiClient by calling the following code:

mClient = new GoogleApiClient.Builder(this) .addApi(Fitness.HISTORY_API) .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE)) .addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE)) .addConnectionCallbacks( new GoogleApiClient.ConnectionCallbacks() { @Override public void onConnected(Bundle bundle) { log.info("FITNESS_API: Connected!!!"); Thread thread = new Thread(new Runnable() { @Override public void run() { insertOrUpdateDataPoints(); } }); thread.start(); } @Override public void onConnectionSuspended(int i) { // If your connection to the sensor gets lost at some point, // you'll be able to determine the reason and react to it here. if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) { log.info("FITNESS_API: Connection lost. Cause: Network Lost."); } else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) { log.info("FITNESS_API: Connection lost. Reason: Service Disconnected"); } } } ).build(); mClient.connect(); 

After creating a DataSet and adding step details as DataPoint elements, I synchronize the information with Google Fit and close GoogleApiClient with

 com.google.android.gms.common.api.Status insertStatus = Fitness.HistoryApi.insertData(mClient, dataSet).await(1, TimeUnit.MINUTES); // Before querying the data, check to see if the insertion succeeded. if (!insertStatus.isSuccess()) { log.info("FITNESS_API: There was a problem inserting the dataset. Status = " + insertStatus.getStatusCode()); } mClient.disconnect(); mClient = null; 

The problem is that when I try to control GoogleApiClient myself (without enableAutoManage), I do not receive an invitation so that the application can send data to Google Fit. This behavior changes if I use enableAutoManage when creating GoogleApiClient. However, in order to enable AutoManage for the client, I need to have an ActivityFragment due to the parameters needed to enableAutoManage. I do not have access to the ActivityFragment in the IntentyService, and I want to keep the client management and the paste action in a separate service that can run in the background.

Also, when I do not use enableAutoManage, although I registered a connect callback for GoogleApiClient, nothing happens.

How can I guarantee that my application will prompt the user to allow the application to send messages to Google Fit? I need this to happen if the application does not have permission to publish to Google Fit when the user opens the application. Any ideas? Thanks.

+7
android google-fit
source share
3 answers

I have found a solution.

If you do not want to use "enableAutoManage", you need to register the onConnectionFailed method as follows:

  @Override public void onConnectionFailed(ConnectionResult connectionResult) { if( !authInProgress ) { try { authInProgress = true; connectionResult.startResolutionForResult( MainActivity.this, REQUEST_OAUTH ); } catch(IntentSender.SendIntentException e ) { } } else { Log.e( "GoogleFit", "authInProgress" ); } } 

A dialog box will appear.

+1
source share

In your intention to use the above method mentioned by @Vlad. Create a notification (sticky or otherwise, depending on your importance) asking the user to give you permission if the connection fails. The notification redirects the user to activity in which you ask the user to give you access to the file again.

0
source share
 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_OAUTH: if (resultCode != Activity.RESULT_OK) { return; } if (!googleApiClient.isConnected()) { googleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL); } else { readDataTask(); } return; case RC_SIGN_IN: GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); if (result.isSuccess()) { GoogleSignInAccount account = result.getSignInAccount(); String email = account.getEmail();//you can get OAuth user email AT THIS POINT. if (GoogleFitService.googleApiClient.isConnected()) { readDataTask(); } } } } 

The first thing you need to do is Oauth Goole accout, and then get your email.

  gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .setAccountName("user email") .requestScopes( new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE), new Scope(Scopes.FITNESS_BODY_READ_WRITE), new Scope(Scopes.FITNESS_NUTRITION_READ_WRITE), new Scope(Scopes.FITNESS_LOCATION_READ_WRITE)) .build(); 

when you get Google Fit data in the background, you need to set up email.

0
source share

All Articles