2016-04-12 51 views
7

我正在嘗試將我的應用程序連接到Google Fit。我正在使用需要執行以下操作的IntentService。獲取有關步驟的信息時開始。在這一點上,我試圖通過調用以下代碼來創建GoogleApiClient:在Android應用程序中處理GoogleFit背景

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,並添加步驟的詳細信息爲數據點elemnets後,我同步的信息,谷歌契合,關閉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健身發佈數據。如果在創建GoogleApiClient時使用enableAutoManage,則此行爲會發生變化。但是,爲了啓用客戶端的自動管理,我需要有一個ActivityFragment,由於enableAutoManage所需的參數。我無法訪問IntentyService中的ActivityFragment,並且我希望將客戶端和插入操作的管理保留在可以在後臺運行的單獨服務中。

此外,當我不使用enableAutoManage,即使我已經註冊了GoogleApiClient的連接回調沒有任何反應。

如何確保我的應用程序提示用戶允許應用發佈到Google健身?如果應用無權在用戶打開該應用時在Google健身中發帖,我需要這樣做。有任何想法嗎?謝謝。

回答

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

這將顯示對話框。

0

在您的意向服務中使用@Vlad提到的上述方法。創建通知(粘滯或以其他方式取決於您的重要性)要求用戶在遇到連接失敗時給予權限。通知會將用戶重定向到要求用戶再次給予fitaccess的活動。

0
@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 accout,然後得到你的電子郵件。

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健身數據時,您需要設置電子郵件。