我实际上是尝试从后台服务中获取Google适合的步骤。让我们调用我的服务FitService.java。
@Override
public void onCreate() {
super.onCreate();
if(mFitnessClient == null){
buildFitnessClient();
Log.d(TAG, "FitnessClient created");
}
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "onHandleIntent");
//block until google fit connects. Give up after 10 seconds.
if (!mFitnessClient.isConnected() && !mFitnessClient.isConnecting()) {
mTryingToConnect = true;
mGoogleApiFitnessClient.connect();
//Wait until the service either connects or fails to connect
while (mTryingToConnect) {
try {
Thread.sleep(100, 0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (mFitnessClient.isConnected()) {
getSteps();
Log.i(TAG, "google api client connected. ");
} else {
//Not connected
Log.e(TAG, "Fit wasn't able to connect, so the request failed.");
}
}
private void buildFitnessClient() {
// Create the Google API Client
mGoogleApiFitnessClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.HISTORY_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE))
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Google Fit connected.");
mTryingToConnect = false;
}
@Override
public void onConnectionSuspended(int i) {
// If your connection to the sensor gets lost at some point,
// you'll be able to determine the reason and react to it here.
mTryingToConnect = false;
if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
Log.i(TAG, "Google Fit Connection lost. Cause: Network Lost.");
} else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
Log.i(TAG, "Google Fit Connection lost. Reason: Service Disconnected");
}
}
}
)
.addOnConnectionFailedListener(
new GoogleApiClient.OnConnectionFailedListener() {
// Called whenever the API client fails to connect.
@Override
public void onConnectionFailed(ConnectionResult result) {
mTryingToConnect = false;
}
}
)
.build();
}
public void getStepsToday(){
long total = 0;
PendingResult<DailyTotalResult> result = Fitness.HistoryApi.readDailyTotal(mGoogleApiFitnessClient, DataType.TYPE_STEP_COUNT_DELTA);
DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
if (totalResult.getStatus().isSuccess()) {
DataSet totalSet = totalResult.getTotal();
total = totalSet.isEmpty()
? 0
: totalSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
Log.i(TAG, "Total steps are the same: " + total);
} else {
Log.e(TAG, "There was a problem while getting the step count.");
}
}
适用于大多数手机设备。但在其他一些设备上,我收到一个奇怪的错误:
Fatal Exception: java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
at com.google.android.gms.common.internal.zzd.zzre(Unknown Source)
at com.google.android.gms.common.internal.zzd.zzrf(Unknown Source)
at com.google.android.gms.internal.zzctu.zza(Unknown Source)
at com.google.android.gms.internal.zzbcd.zzpY(Unknown Source)
at com.google.android.gms.internal.zzbcd.begin(Unknown Source)
at com.google.android.gms.internal.zzbcx.zzd(Unknown Source)
at com.google.android.gms.internal.zzbbi.onConnected(Unknown Source)
at com.google.android.gms.common.internal.zzaa.onConnected(Unknown Source)
at com.google.android.gms.common.internal.zzn.zzj(Unknown Source)
at com.google.android.gms.common.internal.zze.zzrj(Unknown Source)
at com.google.android.gms.common.internal.zzi.zzrk(Unknown Source)
at com.google.android.gms.common.internal.zzh.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
GoogleApiHandler
at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
at android.os.MessageQueue.next(MessageQueue.java:148)
at android.os.Looper.loop(Looper.java:151)
at android.os.HandlerThread.run(HandlerThread.java:61)
有任何线索吗?