android - How to retrieve daily running and walking steps from Google Fit API -
it newbie question have lost 1 day figure out w/o success. i'm using google fit api android app , need show of data running , walking daily steps. have managed show data in time unit (f.e running in x min). need show in steps unit.
the snipped below code shows how retrieved data time unit (in milliseconds):
datareadrequest readrequest = new datareadrequest.builder() .aggregate(datatype.type_activity_segment, datatype.aggregate_activity_summary) .bucketbytime(1, timeunit.days) .settimerange(start, end, timeunit.milliseconds) .build(); fitness.historyapi.readdata(client, readrequest).setresultcallback(new resultcallback<datareadresult>() { @override public void onresult(datareadresult datareadresult) { if (datareadresult.getbuckets().size() > 0) { display.show("bucket dataset.size(): " + datareadresult.getbuckets().size()); retrievehistorydata(datareadresult, client.getcontext()); } } ..... private void retrievehistorydata(datareadresult datareadresult, context context) { int walking = 0; int running = 0; int biking = 0; (bucket bucket : datareadresult.getbuckets()) { list<dataset> datasets = bucket.getdatasets(); (dataset dataset : datasets) { display.show("dataset.datatype: " + dataset.getdatatype().getname()); (datapoint dp : dataset.getdatapoints()) { walking += getbucketdata(dp, fitnessactivities.walking); running += getbucketdata(dp, fitnessactivities.running); biking += getbucketdata(dp, fitnessactivities.biking); } } } log.d(tag, constants.key_walking_steps + ": " + walking); log.d(tag, constants.key_running_steps + ": " + running); log.d(tag, constants.key_biking_steps + ": " + biking); } private int getbucketdata(datapoint dp, string activityname) { int count = 0; (field field : dp.getdatatype().getfields()) { log.d(tag, "field " + field.getname() + " " + fitnessactivities.getname(dp.getvalue(field).asint()) + " = " + dp.getvalue(field).asint() + " field.describecontents()" + field.describecontents()); if ("activity".equals(field.getname()) && activityname.contentequals(fitnessactivities.getname(dp.getvalue(field).asint()))) { count += dp.getvalue(field).asint(); } } return count; }
tnx in advance.
best regards
mb
you can steps various activities each day using following code
datareadrequest readrequest = new datareadrequest.builder() .aggregate(estimated_step_deltas, datatype.aggregate_step_count_delta) .bucketbyactivitytype(1, timeunit.seconds) .settimerange(starttime, endtime, timeunit.milliseconds) .build();
where estimated_step_deltas defined as
datasource estimated_step_deltas = new datasource.builder() .setdatatype(datatype.type_step_count_delta) .settype(datasource.type_derived) .setstreamname("estimated_steps") .setapppackagename("com.google.android.gms") .build();
you can run code in loop data many days want. return data buckets activities have steps involved , can fetch activity name bucket method returns activity name string
bucket.getactivity();
hope helps! :)
Comments
Post a Comment