2016-01-20 75 views
2

我已經做了幾天的研究。我需要的是我的應用程序中的一個簡單的TextView區域,用於顯示今天的步驟。如何使用適用於Android的Google Fit API查找步驟?

我設法使用下面的代碼來獲得認證。它彈出要求許可,並認爲我選擇了正確的一個。

但我無法弄清楚如何簡單獲得Step Count信息。我希望這只是幾行代碼。任何幫助,將不勝感激。謝謝

編輯1:我只需要得到步數數。我可以弄清楚以後如何顯示它。我也有Toasts,以幫助我弄清楚發生了什麼。

private void buildFitnessClient() { 
    if (mClient == null) { 
     mClient = new GoogleApiClient.Builder(this) 
       .addApi(Fitness.SENSORS_API) 
       .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ)) 
       .addConnectionCallbacks(
         new GoogleApiClient.ConnectionCallbacks() { 
          @Override 
          public void onConnected(Bundle bundle) { 
           Log.i(TAG, "Connected!!!"); 
           // Now you can make calls to the Fitness APIs. 
           Toast.makeText(getBaseContext(), "Connected!", Toast.LENGTH_LONG).show(); 


          } 

          @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.i(TAG, "Connection lost. Cause: Network Lost."); 
            Toast.makeText(getBaseContext(), "Connection lost. Cause: Network Lost.", Toast.LENGTH_LONG).show(); 

           } else if (i 
             == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) { 
            Log.i(TAG, 
              "Connection lost. Reason: Service Disconnected"); 
           } 
          } 
         } 
       ) 
       .enableAutoManage(this, 0, new GoogleApiClient.OnConnectionFailedListener() { 
        @Override 
        public void onConnectionFailed(ConnectionResult result) { 
         Log.i(TAG, "Google Play services connection failed. Cause: " + 
           result.toString()); 
         Toast.makeText(getBaseContext(), "Google Play services connection failed. Cause: " + 
           result.toString(), Toast.LENGTH_LONG).show(); 

        } 
       }) 
       .build(); 
    } 
} 
+1

您是否嘗試過使用'HistoryAPI'爲谷歌API健身。 僅供參考:https://developers.google.com/fit/android/history – zIronManBox

+0

Chck this tutotial http://www.gadgetsaint.com/android/google-fit-steps-calories-android/#.WESRX6J97BJ – ASP

回答

1

看看這個official documentation從谷歌如何從飛度讀取數據:在GitHub上

// Setting a start and end date using a range of 1 week before this moment. 
Calendar cal = Calendar.getInstance(); 
Date now = new Date(); 
cal.setTime(now); 
long endTime = cal.getTimeInMillis(); 
cal.add(Calendar.WEEK_OF_YEAR, -1); 
long startTime = cal.getTimeInMillis(); 

java.text.DateFormat dateFormat = getDateInstance(); 
Log.i(TAG, "Range Start: " + dateFormat.format(startTime)); 
Log.i(TAG, "Range End: " + dateFormat.format(endTime)); 

DataReadRequest readRequest = new DataReadRequest.Builder() 
     // The data request can specify multiple data types to return, effectively 
     // combining multiple data queries into one call. 
     // In this example, it's very unlikely that the request is for several hundred 
     // datapoints each consisting of a few steps and a timestamp. The more likely 
     // scenario is wanting to see how many steps were walked per day, for 7 days. 
     .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA) 
     // Analogous to a "Group By" in SQL, defines how data should be aggregated. 
     // bucketByTime allows for a time span, whereas bucketBySession would allow 
     // bucketing by "sessions", which would need to be defined in code. 
     .bucketByTime(1, TimeUnit.DAYS) 
     .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS) 
     .build(); 

歷史API示例應用程序:

檢查樣品項目在GitHub上here。包含所需的代碼

直接鏈接到MainActivity.java(在上面的示例項目):Link

+0

好累這並不會帶來任何錯誤。在此之後,我使用此代碼將其傳遞給結果。 DataReadResult dataReadResult = Fitness.HistoryApi.readData(mClient,readRequest).await(1,TimeUnit.MINUTES);儘管如此,我仍然無法將它傳遞給TextView,甚至不能傳遞。 – Rueben

+0

它能讓你獲得數據嗎? – AndroidMechanic

+0

我不知道如何檢查。我怎樣才能讀取數據結果。我嘗試過,但沒有運氣,因爲我無法將數據集傳遞給它。 https://developers.google.com/fit/android/history#read_daily_total_data – Rueben

相關問題