【发布时间】:2014-04-23 03:31:42
【问题描述】:
我按照以下步骤在我的应用程序(Google Drive Android API)中登录 Drive: https://developers.google.com/drive/android/auth
我使用 debug.keystore 生成了 SHA1 指纹。当我在我的设备上运行应用程序时,第一次连接失败,但 hasResolution 为真,并出现帐户选择对话框。问题是onActivityResult方法在我选择账户之前被立即调用,resultCode当然不行。
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;
}
}
【问题讨论】:
-
如果这是现在发生的事情,Google Drive / Hangouts / Talk 已经关闭了 2 个小时(至少在纽约市),这可以解释为什么它会立即失败
-
从远处看,代码没有任何问题。我不明白为什么你的'onActivityResult 方法被立即调用'。用户在“AccountManager.KEY_ACCOUNT_NAME”中使用所选电子邮件点击确定后,它应该从帐户选择器返回(离开/返回时通过 onStop(),onStart())。
-
尝试将
mGoogleApiClient.connect()从 onStart() 移动到 onResume()。
标签: android google-drive-api google-authentication google-drive-android-api