【问题标题】:Google Fit Android API Acitivity Time?Google Fit Android API 活动时间?
【发布时间】:2018-05-09 21:26:48
【问题描述】:

我目前正在使用 Google Fit Android API 来访问我的应用程序中的步数数据。在 Google fit android 应用上,它有一个活动时间值Attached image of the value I am trying to access

我尝试了多种方法来访问我的应用程序中的这些数据,但我不知道我应该使用哪种瞬时数据类型?或者如果我完全错了,需要尝试不同的方法?

任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: android google-fit


    【解决方案1】:

    我有同样的疑问,我使用了这个参考https://developers.google.com/fit/android/history 我想我有可能的解决方案,在这种情况下,我将时间间隔设置为从午夜到现在。您需要使用bucketByActivitySegment创建一个DataReadRequest

    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 变量显示每个健身活动的活动时间,您应该查看此参考 https://developers.google.com/android/reference/com/google/android/gms/fitness/FitnessActivities,因为列出的活动之一是 STILL:用户仍然(不移动)。

    我希望这对你有所帮助。

    【讨论】:

      【解决方案2】:

      google fit 中的每个数据集都有开始时间和结束时间。您只需减去它们即可获得所花费的时间。下面是一个例子。

       if (dataReadResult.getBuckets().size() > 0) {
          for (Bucket bucket : dataReadResult.getBuckets()) {
              long activeTime = bucket.getEndTime(TimeUnit.MINUTES) - bucket.getStartTime(TimeUnit.MINUTES);
              Log.i(TAG, "Active time " + activeTime);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 2017-03-28
        • 1970-01-01
        相关资源
        最近更新 更多