本文介绍了onConnectionFailed给出SIGN_IN_REQUIRED(4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序,我想使用Google API。为此,我导入了 google-play-service-lib 项目。 我正在关注这个来初始化GoogleApiClient对象。



我的代码:
$ b $ 1 在 onCreate()方法中,我构建了 GoogleApiClient 对象:

  mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this )
.addApi(Plus.API,null)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();



onStart()中,I调用 mGoogleApiClient.connect()


$ b $ 3我的活动实现了
ConnectionCallbacks OnConnectionFailedListener。


$ b 我的 onConnectionFailed()
  public void onConnectionFailed(ConnectionResult result){
//在dubug结果中看起来像:ConnectionResult {
// statusCode = SIGN_IN_REQUIRED,resolution = PendingIntent
// {41f8ca70:android.os.BinderProxy@41f8ca10}}
尝试{
if(!mIntentInProgress&& result.hasResolution() )
{
mIntentInProgress = true;
result.startResolutionForResult(mActivity,RC_SIGN_IN);
}

} catch(SendIntentException e){
e.printStackTrace();


$ / code $ / pre
$ b 我的 onActivityResult( )方法包含:

  if(requestCode == RC_SIGN_IN){
if(! mGoogleApiClient.isConnecting()){
mGoogleApiClient.connect();


$ / code $ / pre

当我运行我的应用程序时,我得到一个吐司表示内部错误弹出。我在Google控制台中创建了该项目。

解决方案

我有同样的问题。 b $ b

来自文档:
客户端可以选择继续而不使用API​​,或者可以调用startResolutionForResult(Activity,int)来提示用户登录。



因此,您应该尝试使用startResolutionForResult()函数登录
$ b $ pre $ @覆盖
public void onConnectionFailed(ConnectionResult result){
if(result.hasResolution()){
try {
// !!!
result.startResolutionForResult(this,REQUEST_CODE_RESOLVE_ERR);
} catch(SendIntentException e){
mPlusClient.connect();
}
}

mConnectionResult = result;
}


I am developing an Android application where I want to use the Google API. For that I have imported the google-play-service-lib project.

I am following this link to initialize GoogleApiClient object.

My code:

1) In the onCreate() method I am building the GoogleApiClient object:

mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .addApi(Plus.API, null)
    .addScope(Plus.SCOPE_PLUS_LOGIN)
    .build();

2) In onStart(), I call mGoogleApiClient.connect().

3) My activity implementsConnectionCallbacks and OnConnectionFailedListener.

4) My onConnectionFailed() method looks like:

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) My onActivityResult() method contains:

if (requestCode == RC_SIGN_IN) {
   if (!mGoogleApiClient.isConnecting()) {
      mGoogleApiClient.connect();
   }
}

When I run my app I get a Toast that says that an internal error popped up. I did create the project in the Google console.

解决方案

I had the same problem.

From documentation:The client may choose to continue without using the API or it may call startResolutionForResult(Activity, int) to prompt the user to sign in.

So you should try to sign in by using startResolutionForResult() function

@Override
public void onConnectionFailed(ConnectionResult result) {
   if (result.hasResolution()) {
       try {
           // !!!
           result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
       } catch (SendIntentException e) {
           mPlusClient.connect();
       }
   }

   mConnectionResult = result;
}

这篇关于onConnectionFailed给出SIGN_IN_REQUIRED(4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 12:28