所以我正在尝试创建一个连接到Google Fit的应用程序,并以非常精简的方式向用户显示他们的数据,而且我很难找到用户每天为每个活动消耗的卡路里.我可以获得一整天的总卡路里,以及用户每天所做的每项活动,但不能获得每项活动消耗的卡路里.
链接到GitHub:https://github.com/drb56/FitTest
我只添加了java代码而不是任何xml的东西. Google Fit代码位于FitTestFragment.java中.
我将在下面粘贴一些关键代码:
这是我连接到google fit API客户端的位置:
mClient = new GoogleApiClient.Builder(getContext()) .addApi(Fitness.HISTORY_API) .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ)) .addConnectionCallbacks(this) .enableAutoManage(getActivity(),new GoogleApiClient.OnConnectionFailedListener() { @Override public void onConnectionFailed(ConnectionResult result) { Log.i("MyApp","Google Play services connection Failed. Cause: " + result.toString()); }}) .build();
这是我为特定信息制作DataReadRequest的地方:
DataReadRequest readRequest = new DataReadRequest.Builder() .aggregate(DataType.TYPE_CALORIES_EXPENDED,DataType.AGGREGATE_CALORIES_EXPENDED) .aggregate(DataType.TYPE_ACTIVITY_SEGMENT,DataType.AGGREGATE_ACTIVITY_SUMMARY) .bucketByTime(1,TimeUnit.DAYS) .setTimeRange(startTime,endTime,TimeUnit.MILLISECONDS) .build(); DataReadResult dataReadResult = Fitness.HistoryApi.readData(mClient,readRequest).await(1,TimeUnit.MINUTES); if (dataReadResult.getBuckets().size() > 0) { Log.i("MyApp","Number of returned buckets of DataSets is: " + dataReadResult.getBuckets().size()); for (Bucket bucket : dataReadResult.getBuckets()) { List<DataSet> dataSets = bucket.getDataSets(); for (DataSet dataSet : dataSets) { dumpDataSet(dataSet); } } } else { Log.i("MyApp","No data"); }
以下是我的输出对于某些活动和消耗的卡路里的影响:
Data point: Type: com.google.calories.expended Date: 06/09/2016 Start: 2:58:13 PM End: 2:58:13 PM Field: calories Value: 2555.9749 Data point: Type: com.google.activity.summary Date: 06/09/2016 Start: 2:58:13 PM End: 2:58:13 PM Field: activity Value: 3 Field: duration Value: 76513626 Field: num_segments Value: 17 Data point: Type: com.google.activity.summary Date: 06/09/2016 Start: 4:13:58 PM End: 12:41:04 PM Field: activity Value: 7 Field: duration Value: 4553146 Field: num_segments Value: 17
解决方法
您可以使用以下代码获取每天各种活动的卡路里
DataReadRequest readRequest = new DataReadRequest.Builder() .aggregate(DataType.TYPE_CALORIES_EXPENDED,DataType.AGGREGATE_CALORIES_EXPENDED) .bucketByActivityType(1,TimeUnit.SECONDS) .setTimeRange(startTime,TimeUnit.MILLISECONDS) .build();
您可以在循环中运行此代码,以根据需要获取数天.它将返回所有活动的数据存储桶,您可以通过此方法从存储桶中获取活动名称,该方法将活动名称返回为String
bucket.getActivity();
将它存储在一个自定义的集合中,你很高兴.希望能帮助到你! 原文链接:https://www.f2er.com/java/128559.html