我按照以下步骤在我的应用程序(Google Drive Android API)中登录 Drive: https ://developers.google.com/drive/android/auth
我使用 debug.keystore 生成了 SHA1 指纹。当我在我的设备上运行应用程序时,第一次连接失败,但 hasResolution 为真,并出现帐户选择对话框。问题是在我选择一个帐户之前立即调用了 onActivityResult 方法,结果代码当然是不正确的。
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//some other code...
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
if(mGoogleApiClient != null)
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "DRIVE: connected");
CONNECTED = true;
}
@Override
public void onConnectionSuspended(int i) {
Log.d(TAG, "DRIVE: disconnected");
CONNECTED = false;
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "DRIVE: connection failed");
CONNECTED = false;
if (connectionResult.hasResolution()) {
try {
connectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
} catch (IntentSender.SendIntentException e) {
//mGoogleApiClient.connect(); whit or whitout is the same...
}
} else {
GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show();
}
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data){
switch (requestCode) {
case REQUEST_CODE_RESOLUTION:
if (resultCode == RESULT_OK) {
mGoogleApiClient.connect();
}
break;
case REQUEST_CODE_CREATOR:
if(resultCode != RESULT_OK)
newNotification("Upload Fallito", "...", android.R.drawable.ic_dialog_alert);
break;
}
}