我正在开发一个想要使用 Google API 的 Android 应用程序。为此,我导入了 google-play-service-lib 项目。
我正在关注 link初始化 GoogleApiClient 对象。
我的代码:
1) 在 onCreate()
方法中,我正在构建 GoogleApiClient 对象:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API, null)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
2) 在onStart()
中,我调用mGoogleApiClient.connect()
。
3) 我的 Activity 实现 ConnectionCallbacks 和 OnConnectionFailedListener.
4) 我的 onConnectionFailed()
方法如下所示:
public void onConnectionFailed(ConnectionResult result) {
//in dubug result looks like : ConnectionResult{
//statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent
// {41f8ca70: android.os.BinderProxy@41f8ca10}}
try {
if(!mIntentInProgress && result.hasResolution())
{
mIntentInProgress=true;
result.startResolutionForResult(mActivity, RC_SIGN_IN);
}
} catch (SendIntentException e) {
e.printStackTrace();
}
}
5) 我的 onActivityResult()
方法包含:
if (requestCode == RC_SIGN_IN) {
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
当我运行我的应用程序时,我得到一个 Toast 表示弹出了一个内部错误。我确实在 Google 控制台中创建了该项目。
最佳答案
我遇到了同样的问题。
来自文档: 客户端可以选择不使用 API 继续,也可以调用 startResolutionForResult(Activity, int) 提示用户登录。
所以你应该尝试使用 startResolutionForResult() 函数登录
@Override
public void onConnectionFailed(ConnectionResult result) {
if (result.hasResolution()) {
try {
// !!!
result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
} catch (SendIntentException e) {
mPlusClient.connect();
}
}
mConnectionResult = result;
}
关于android - onConnectionFailed 给 SIGN_IN_REQUIRED(4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23736137/