android - onConnectionFailed 给 SIGN_IN_REQUIRED(4)

标签 android google-play google-play-services

我正在开发一个想要使用 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 实现 ConnectionCallbacksOnConnectionFailedListener.

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/

相关文章:

android - Google Play 服务每次都会对版本发出警告

Android 设备的日志文件

android - 播放商店 alpha 测试下载链接不起作用

java - FileUtils.copyFile() VS FileChannel.transferTo()

android - 如何用Java代码解析上传的apk文件?

android - 购买一个应用程序以启用另一个应用程序的付费功能如何运作?

android - Android 中的 Google 登录按钮错误

java - 带有 Google Play 服务的 Android 地理围栏 6.5.87

java - 如何创建 "30 second countdown timer"?

android - 图像和文本居中的 GridView