我正在尝试使用 Fitness api 在 android 上显示上周的数据,但出现以下错误:
E/AndroidRuntime:致命异常:主进程:com.example.admin.safely_counter,PID:7435 java.lang.IllegalStateException:当时间大于零时,不得在 UI 线程上调用 await。在 com.google.android.gms.common.internal.Preconditions.checkNotMainThread(Unknown Source) 在 com.google.android.gms.common.api.internal.BasePendingResult.await(Unknown Source) 在 com.example.admin.safely_counter .MainActivity.displayLastWeeksData(MainActivity.java:99) 位于 com.google.android.gms.common.internal.GmsClientEventManager.onConnectionSuccess 的 com.example.admin.safely_counter.MainActivity.onConnected(MainActivity.java:149)在 com.google。
MainActivity.java
public class MainActivity extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
View.OnClickListener {
private Button mButtonViewWeek;
private Button mButtonViewToday;
private Button mButtonAddSteps;
private Button mButtonUpdateSteps;
private Button mButtonDeleteSteps;
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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(this);
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(){
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.WEEK_OF_YEAR, -1);
long startTime = cal.getTimeInMillis();
java.text.DateFormat dateFormat = DateFormat.getDateInstance();
Log.e("History", "Range Start: " + dateFormat.format(startTime));
Log.e("History", "Range End: " + dateFormat.format(endTime));
//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(startTime, endTime, 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));
}
}
}
@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");
displayLastWeeksData();
}
@Override
public void onClick(View v) {
}
}