2016-01-08 40 views

回答

1

我有同樣的疑問,我用這個參考https://developers.google.com/fit/android/history我想我有可能的解決方案,在這種情況下,我設置從午夜到現在的時間間隔。你需要創建一個使用bucketByActivitySegment

private class VerifyDataTask extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected Void doInBackground(Void... voids) { 
     // Begin by creating the query. 
     DataReadRequest readRequest = queryFitnessData(); 

     // [START read_dataset] 
     // Invoke the History API to fetch the data with the query and await the result of 
     // the read request. 
     DataReadResult dataReadResult = 
       Fitness.HistoryApi.readData(mClient, readRequest).await(1, TimeUnit.MINUTES); 
     // [END read_dataset] 

     // For the sake of the sample, we'll print the data so we can see what we just added. 
     // In general, logging fitness information should be avoided for privacy reasons. 
     printData(dataReadResult); 

     return null; 
    } 
} 

public static DataReadRequest queryFitnessData() { 
    // [START build_read_data_request] 
    // 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.set(Calendar.HOUR_OF_DAY, 0); 
    cal.set(Calendar.MINUTE, 0); 
    cal.set(Calendar.SECOND, 0); 
    long startTime = cal.getTimeInMillis(); 

    DataSource ACTIVITY_SEGMENT = new DataSource.Builder() 
      .setDataType(DataType.TYPE_ACTIVITY_SEGMENT) 
      .setType(DataSource.TYPE_DERIVED) 
      .setStreamName("estimated_activity_segment") 
      .setAppPackageName("com.google.android.gms") 
      .build(); 

    DataReadRequest readRequest = new DataReadRequest.Builder() 
      .aggregate(ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY) 
      .bucketByActivitySegment(1, TimeUnit.SECONDS) 
      .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS) 
      .build(); 
    // [END build_read_data_request] 

    return readRequest; 
} 

public static void printData(DataReadResult dataReadResult) { 
    // [START parse_read_data_result] 
    // If the DataReadRequest object specified aggregated data, dataReadResult will be returned 
    // as buckets containing DataSets, instead of just DataSets. 
    if (dataReadResult.getBuckets().size() > 0) { 
     Log.i(TAG, "Number of returned buckets of DataSets is: " 
       + dataReadResult.getBuckets().size()); 
     for (Bucket bucket : dataReadResult.getBuckets()) { 
      Log.i(TAG, bucket.getActivity()); 
      long activeTime = bucket.getEndTime(TimeUnit.MINUTES) - bucket.getStartTime(TimeUnit.MINUTES); 
      Log.i(TAG, "Active time " + activeTime); 
     } 
    } 
    // [END parse_read_data_result] 
} 

的activeTime變量顯示每個健身活動的活動時間一DataReadRequest,你應該看看這個參考https://developers.google.com/android/reference/com/google/android/gms/fitness/FitnessActivities因爲上市活動之一是STILL:用戶仍然(不移動)。

我希望這對你有幫助。

0

谷歌健身中的每個數據集都有開始時間和結束時間。你可以通過僅將它們進行分解來獲得時間。