Java – Google fit API to get the calories consumed by each activity
So I'm trying to create an app that connects to Google fit and displays their data to users in a very compact way, and it's hard for me to find the calories users burn for each activity every day I can get the total calories of the whole day and every activity the user does every day, but I can't get the calories consumed by each activity
Link to GitHub: https://github.com/drb56/FitTest I only added java code, not anything XML The Google fit code is located at fittestfragment Java I'll paste some key code below:
This is where I connect to the Google fit API client:
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();
This is where I make a datareadrequest for specific information:
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"); }
Here are the effects of my output on some activities and calories consumed:
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
Solution
You can use the following code to get calories for various activities every day
DataReadRequest readRequest = new DataReadRequest.Builder() .aggregate(DataType.TYPE_CALORIES_EXPENDED,DataType.AGGREGATE_CALORIES_EXPENDED) .bucketByActivityType(1,TimeUnit.SECONDS) .setTimeRange(startTime,TimeUnit.MILLISECONDS) .build();
You can run this code in a loop to get a few days as needed It will return all active data buckets. You can get the activity name from the bucket through this method, which returns the activity name as string
bucket.getActivity();
You'll be happy to store it in a custom collection Hope to help you!
The above is the Java – Google fit API collected by programming house for you to obtain all the contents of calories consumed by each activity. I hope this article can help you solve the program development problems encountered by Java – Google fit API to obtain calories consumed by each activity.
If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.