【问题标题】:Handle GoogleFit in background in Android App在 Android 应用程序的后台处理 GoogleFit
【发布时间】:2016-04-12 11:47:12
【问题描述】:

我正在尝试将我的应用连接到 Google 健身。我正在使用需要执行以下操作的 IntentService。当我有关于步骤的信息时开始。此时我正在尝试通过调用以下代码来创建 GoogleApiClient:

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();

在创建 DataSet 并将步骤详细信息添加为 DataPoint elemnets 后,我将信息同步到 Google Fit 并关闭 GoogleApiClient:

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;

问题在于,通过尝试自行管理 GoogleApiClient(没有 enableAutoManage),我没有收到允许应用将数据发布到 Google Fit 的提示。如果我在创建 GoogleApiClient 时使用 enableAutoManage,此行为会发生变化。但是,为了为客户端启用 AutoManage,由于 enableAutoManage 所需的参数,我需要一个 ActivityFragment。我无权访问 IntentyService 中的 ActivityFragment,我确实希望将客户端的管理和插入操作保留在可以在后台运行的单独服务中。

此外,即使我已经为 GoogleApiClient 注册了连接回调,但我不使用 enableAutoManage 时也没有任何反应。

如何确保我的应用程序提示用户允许该应用程序发布到 Google Fit?如果用户打开应用时应用无权在 Google Fit 中发帖,我需要这样做。有任何想法吗?谢谢。

【问题讨论】:

    标签: android google-fit


    【解决方案1】:

    我找到了解决办法。

    如果你不想使用“enableAutoManage”,你需要像这样注册onConnectionFailed方法:

        @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" );
        }
    }
    

    这将显示对话框。

    【讨论】:

      【解决方案2】:

      在您的意图服务中使用@Vlad 提到的上述方法。创建一个通知(粘滞或其他取决于您的重要性)在遇到连接失败时要求用户授予您权限。该通知会将用户重定向到您要求用户再次为您提供 fitaccess 的活动。

      【讨论】:

        【解决方案3】:
        @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's email AT THIS POINT.
        
                        if (GoogleFitService.googleApiClient.isConnected()) {
                            readDataTask();
                        }
                    }
            }
        }
        

        第一次您要做的是 Oauth Goole 帐户,然后获取您的电子邮件。

         gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                        .setAccountName("user's 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();
        

        当您在后台获取 Google Fit 数据时,您需要设置电子邮件。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-09-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多