2017-09-13 83 views
0

我目前正在開發相關的健身數據的Android應用程序,該應用程序我需要得到STEP_COUNT,HEART_RATE_BPM,睡眠時間和鍛鍊data.I需要獲取數據每小時所以我使用bucketByTime爲1小時,我使用AGGREGATE_ACTIVITY_SUMMARY爲withing 1 hour.I所有活動獲取活動的彙總數據還可以過濾來自但是API中列出了不實用的,因爲有太多的運動類型的運動數據。我正在尋找替代解決方案。谷歌適合在一個小時之內得到鍛鍊數據的Android

 DataReadRequest readRequest = new DataReadRequest.Builder() 
       .aggregate(DataType.TYPE_STEP_COUNT_DELTA, 
         DataType.AGGREGATE_STEP_COUNT_DELTA) 
       .aggregate(DataType.TYPE_HEART_RATE_BPM, DataType.AGGREGATE_HEART_RATE_SUMMARY) 
       .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY) 
       .bucketByTime(1, TimeUnit.HOURS) 
       .setTimeRange(starttime, endtime, TimeUnit.MILLISECONDS) 
       .build(); 
     DataReadResult dataReadResult = Fitness.HistoryApi.readData(mClient, readRequest).await(1, TimeUnit.MINUTES); 

     //Used for aggregated data 
     if (dataReadResult.getBuckets().size() > 0) { 
      Log.i("History", "Number of buckets: " + dataReadResult.getBuckets().size()); 
      for (Bucket bucket : dataReadResult.getBuckets()) { 
       List<DataSet> dataSets = bucket.getDataSets(); 
       for (DataSet dataSet : dataSets) { 
        showDataSet(dataSet); 
       } 
      } 
     } 


     return null; 
    } 
} 

private void readData() { 
    new VerifyDataTask().execute(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the main; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
    private void showDataSet(DataSet dataSet) { 
     Log.e("History", "Data returned for Data type: " + dataSet.getDataType().getName()); 
     DateFormat dateFormat = DateFormat.getDateInstance(); 
     DateFormat timeFormat = DateFormat.getTimeInstance(); 
     String sleepHours = "0"; 

     for (DataPoint dp : dataSet.getDataPoints()) { 
      Log.i("History", "Data point:"); 
      Log.i("History", "\tType: " + dp.getDataType().getName()); 
      Log.i("History", "\tStart: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS))); 
      Log.i("History", "\tEnd: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS))); 
      for(Field field : dp.getDataType().getFields()) { 

       if(dp.getOriginalDataSource().getAppPackageName().toString().contains("sleep") && field.getName().contains("duration")){ 
        Value value = dp.getValue(field); 
        long h = TimeUnit.MILLISECONDS.toHours(value.asInt()); 
        long m =TimeUnit.MILLISECONDS.toMinutes(value.asInt()); 
        m = m - (h*60); 
        sleepHours =h +" h " + m + " m"; 
        Log.i(TAG, "\tField: Sleep duration : " + sleepHours); 

       } 

       Log.i("History", "\tField: " + field.getName() + 
         " Value: " + dp.getValue(field)); 
      } 
     } 

    } 

回答

1

你可以參考這個documentation如何閱讀詳細和彙總數據。在示例代碼中,您需要使用此時刻之前1周的範圍設置開始日期和結束日期。 cal.add(Calendar.WEEK_OF_YEAR, -1);。如果您只想要1小時,請使用cal.add(Calendar.HOUR_OF_DAY, -1);。創建DataReadRequest實例後,使用[HistoryApi.readData()https://developer.android.com/reference/com/google/android/gms/fitness/HistoryApi.html#readData(com.google.android.gms.common.api.GoogleApiClient,com.google.android.gms.fitness.request.DataReadRequest))方法,並等待同步或提供一個回調方法來處理從健身的歷史數據。