While running the code on my button click app crashes instantly. Log shows following error
Caused by: java.lang.IllegalStateException: await must not be called on the UI thread when time is greater than zero.
at com.google.android.gms.common.internal.zzbo.zza(Unknown Source)
at com.google.android.gms.internal.zzbbd.await(Unknown Source)
at com.gfit.test.mygfitapp.MainActivity.onStart(MainActivity.java:154)
Button click method contains following codes.
Calendar calendar = Calendar.getInstance();
Date date = new Date();
calendar.setTime(date);
long endTime = calendar.getTimeInMillis();
calendar.add(Calendar.YEAR, -1);
long startTime = calendar.getTimeInMillis();
DataReadRequest dataReadRequest = new DataReadRequest.Builder()
.read(DataType.TYPE_WEIGHT)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.setLimit(1)
.build();
DataReadResult dataReadResult = Fitness.HistoryApi.readData(mApiClient, dataReadRequest).await(1000, TimeUnit.MILLISECONDS);
Toast.makeText(getApplicationContext(),""+dataReadResult.toString(),Toast.LENGTH_LONG).show();
As mentioned in log it can't be run on ui thread . What is the alternate methode to run the code.