我正在尝试为我的应用提供Google健身历史记录api,我想在手机屏幕上显示日常操作,但我不知道该怎么做。如果能对此有所帮助,并在可能的情况下分别显示每天的数据,那将更好。目前,如您所见,我正在获取日志。
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.hello);
mButtonViewWeek = (Button) findViewById(R.id.btn_view_week);
mButtonViewToday = (Button) findViewById(R.id.btn_view_today);
mButtonAddSteps = (Button) findViewById(R.id.btn_add_steps);
mButtonUpdateSteps = (Button) findViewById(R.id.btn_update_steps);
mButtonDeleteSteps = (Button) findViewById(R.id.btn_delete_steps);
mButtonViewWeek.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ViewWeekStepCountTask().execute();
}
});
mButtonViewToday.setOnClickListener(this);
mButtonAddSteps.setOnClickListener(this);
mButtonUpdateSteps.setOnClickListener(this);
mButtonDeleteSteps.setOnClickListener(this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.HISTORY_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addConnectionCallbacks(this)
.enableAutoManage(this, 0, this)
.build();
}
private class ViewWeekStepCountTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
displayLastWeeksData();
return null;
}
}
检索数据:
public void displayLastWeeksData(){
Date date1 = new Date();
SimpleDateFormat formatter1 = new SimpleDateFormat("MMddyyyy");
String format = formatter1.format(date1);
SimpleDateFormat formatter = new SimpleDateFormat("MMddyyyy hh:mm:ss", Locale.ENGLISH);
Calendar cal = Calendar.getInstance();
Date today = null;
try {
today = formatter.parse(format+" 00:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
cal.setTime(today);
cal.add(Calendar.MONTH, -1);
long start = cal.getTimeInMillis();
System.out.println("start time:"+start);
Date date= null;
try {
date = formatter.parse(format+" 23:59:59");
} catch (ParseException e) {
e.printStackTrace();
}
cal.setTime(date);
long end = cal.getTimeInMillis();
System.out.println("end time: "+end);
Date tem= new Date();
cal.setTime(tem);
long present = cal.getTimeInMillis();
System.out.println(present);
cal.setTime(today);
java.text.DateFormat dateFormat = DateFormat.getDateInstance();
Log.e("History", "Range Start: " + dateFormat.format(start));
Log.e("History", "Range End: " + dateFormat.format(end));
//Check how many steps were walked and recorded in the last 7 days
final DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(start, end, TimeUnit.MILLISECONDS)
.build();
final DataReadResult dataReadResult = Fitness.HistoryApi.readData(mGoogleApiClient, readRequest).await(1,TimeUnit.MINUTES);
//Used for aggregated data
if (dataReadResult.getBuckets().size() > 0) {
Log.e("History", "Number of buckets: " + dataReadResult.getBuckets().size());
for (Bucket bucket : dataReadResult.getBuckets()) {
List<DataSet> dataSets = bucket.getDataSets();
for (DataSet dataSet : dataSets) {
showDataSet(dataSet);
}
}
}
//Used for non-aggregated data
else if (dataReadResult.getDataSets().size() > 0) {
Log.e("History", "Number of returned DataSets: " + dataReadResult.getDataSets().size());
for (DataSet dataSet : dataReadResult.getDataSets()) {
showDataSet(dataSet);
}
}
}
用于显示值:
private void showDataSet(DataSet dataSet) {
Log.e("History", "Data returned for Data type: " + dataSet.getDataType().getName());
DateFormat dateFormat = DateFormat.getDateInstance();
DateFormat timeFormat = DateFormat.getTimeInstance();
for (DataPoint dp : dataSet.getDataPoints()) {
Log.e("History", "Data point:");
Log.e("History", "\tType: " + dp.getDataType().getName());
Log.e("History", "\tStart: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
Log.e("History", "\tEnd: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
for (Field field : dp.getDataType().getFields()) {
Log.e("History", "\tField: " + field.getName() +
" Value: " + dp.getValue(field));
//textView.setText(dp.getValue(field).toString());
}
}
}
@Override
public void onConnectionSuspended(int i) {
Log.e("HistoryAPI", "onConnectionSuspended");
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e("HistoryAPI", "onConnectionFailed");
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.e("HistoryAPI", "onConnected");
new ViewWeekStepCountTask().execute();
}
@Override
public void onClick(View v) {
}
}