连接挂起了Android的Google连接

连接挂起了Android的Google连接,android,sdk,connection,Android,Sdk,Connection,我的日志中有一条来自谷歌的有趣消息 Connection lost. Reason: Service Disconnected 下面是我如何使用谷歌的代码来连接他们的服务 private void buildFitnessClient() { // Create the Google API Client mClient = new GoogleApiClient.Builder(this) .addApi(Fitness.SENSORS_API)

我的日志中有一条来自谷歌的有趣消息

Connection lost.  Reason: Service Disconnected
下面是我如何使用谷歌的代码来连接他们的服务

private void buildFitnessClient() {
    // Create the Google API Client
    mClient = new GoogleApiClient.Builder(this)
            .addApi(Fitness.SENSORS_API)
            .addApi(Fitness.HISTORY_API)
            .addApi(Fitness.RECORDING_API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
            .addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE))
            .addScope(new Scope(Scopes.FITNESS_LOCATION_READ))
            .addConnectionCallbacks(
                    new GoogleApiClient.ConnectionCallbacks() {
                        @Override
                        public void onConnected(Bundle bundle) {
                            Log.i(TAG, "Connected!!!");



                            Log.i(TAG, "Connected!!!");

                            new InsertAndVerifyDataTask().execute();


                        }

                        @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.
                            if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
                                Log.i(TAG, "Connection lost.  Cause: Network Lost.");
                            } else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
                                Log.i(TAG, "Connection lost.  Reason: Service Disconnected");
                            }
                        }
                    }
            )
            .addOnConnectionFailedListener(
                    new GoogleApiClient.OnConnectionFailedListener() {
                        // Called whenever the API client fails to connect.
                        @Override
                        public void onConnectionFailed(ConnectionResult result) {
                            Log.i(TAG, "Connection failed. Cause: " + result.toString());
                            if (!result.hasResolution()) {
                                // Show the localized error dialog
                                GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(),
                                        League.this, 0).show();
                                return;
                            }
                            // The failure has a resolution. Resolve it.
                            // Called typically when the app is not yet authorized, and an
                            // authorization dialog is displayed to the user.
                            if (!authInProgress) {
                                try {
                                    Log.i(TAG, "Attempting to resolve failed connection");
                                    authInProgress = true;
                                    result.startResolutionForResult(League.this,
                                            REQUEST_OAUTH);
                                } catch (IntentSender.SendIntentException e) {
                                    Log.e(TAG,
                                            "Exception while starting resolution activity", e);
                                }
                            }
                        }
                    }
            )
            .build();
}
如果您更仔细地查看代码,该消息将出现在

onConnectionSuspended(int i)

方法。换句话说,它不允许我连接。我以前没有这个问题

这很容易,我不得不添加以下代码,但我忘记了

 @Override
protected void onStart() {
    super.onStart();
    active = true;
    mClient.connect();
    //checkLeague(username, password);

}

@Override
protected void onDestroy() {
    super.onDestroy();
    //showUser(username,password);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_OAUTH) {
        authInProgress = false;
        if (resultCode == RESULT_OK) {
            // Make sure the app is not already connected or attempting to connect
            if (!mClient.isConnecting() && !mClient.isConnected()) {
                mClient.connect();
            }
        }
    }
}

@Override
protected void onStop() {
    super.onStop();
    active = false;
}

}

这很容易,我不得不添加下面的代码,我忘记了

 @Override
protected void onStart() {
    super.onStart();
    active = true;
    mClient.connect();
    //checkLeague(username, password);

}

@Override
protected void onDestroy() {
    super.onDestroy();
    //showUser(username,password);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_OAUTH) {
        authInProgress = false;
        if (resultCode == RESULT_OK) {
            // Make sure the app is not already connected or attempting to connect
            if (!mClient.isConnecting() && !mClient.isConnected()) {
                mClient.connect();
            }
        }
    }
}

@Override
protected void onStop() {
    super.onStop();
    active = false;
}

}