其他分享
首页 > 其他分享> > Android Google登录陷入循环

Android Google登录陷入循环

作者:互联网

我正在实现从Fragment登录的Google加号.
我在对话框中遇到Google登录的无限循环.

码:
活动:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    Log.e("TEST", "requestCode: " + requestCode + ", resultCode: " + resultCode);
    if (FragmentLogIn.RC_SIGN_IN == requestCode){
        FragmentLogIn fragment = (FragmentLogIn)  getSupportFragmentManager().findFragmentById(mainContent.getId());
        fragment.onActivityResult(requestCode, resultCode, data);
    } else{
        super.onActivityResult(requestCode, resultCode, data);
    }
}

分段:

@Override
public void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
public void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

/**
 * Method to resolve any signin errors
 * */
private void resolveSignInError() {
    if (mConnectionResult.hasResolution()) {
        try {
            mIntentInProgress = true;
            mConnectionResult.startResolutionForResult(getActivity(), RC_SIGN_IN);
        } catch (SendIntentException e) {
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        }
    }
}

@Override
public void onActivityResult(int requestCode, int responseCode, Intent intent)
{           
    Log.e("GOOGLE+", "requestCode: " + requestCode + "responseCode = " + responseCode);

    if (requestCode == RC_SIGN_IN) {
        if (responseCode != Activity.RESULT_OK) {
            mSignInClicked = false;
        }

        mIntentInProgress = false;

        if (!mGoogleApiClient.isConnecting()) {
            mGoogleApiClient.connect();
        }
    }
}

@Override
public void onConnected(Bundle bundle)
{
    Log.e("GOOGLE TEST", "onConnected");
    //get user info
    try{
        if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
            Person currentPerson = Plus.PeopleApi
                    .getCurrentPerson(mGoogleApiClient);
            String personName = currentPerson.getDisplayName();
            String personPhotoUrl = currentPerson.getImage().getUrl();
            String personGooglePlusProfile = currentPerson.getUrl();
            String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
            new RetrieveTokenTask().execute(email);
        } else {
            Log.e("GOOGLE+", "info = null");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    mSignInClicked = false;
}

@Override
public void onConnectionSuspended(int i)
{
    Log.e("GOOGLE TEST", "onConnectionSuspended");
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    if (!result.hasResolution()) {
        GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), getActivity(),
                0).show();
        return;
    }

    if (!mIntentInProgress) {
        // Store the ConnectionResult for later usage
        mConnectionResult = result;

        if (mSignInClicked) {
            // The user has already clicked 'sign-in' so we attempt to
            // resolve all
            // errors until the user is signed in, or they cancel.
            resolveSignInError();
        }
    }

}

@Override
public void onClick(View view)
{
    if (view.getId() == R.id.google_sign_in_button) {
        if (!mGoogleApiClient.isConnecting()) {
            mSignInClicked = true;
            resolveSignInError();
        }
    } else if (view.getId() == R.id.google_sign_out_button) {
        revokeGplusAccess();

    }
}

private class RetrieveTokenTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        String accountName = params[0];
        Log.e("accountName", accountName);
        String scopes = "oauth2:profile "+Scopes.PLUS_LOGIN;;
        String token = null;
        try {
            if (getActivity() != null)
                token = GoogleAuthUtil.getToken(getActivity().getApplicationContext(), accountName, scopes);
        } catch (IOException e) {
            Log.e("GOOGLE+", e.getMessage());
        } catch (UserRecoverableAuthException e) {
            startActivityForResult(e.getIntent(), 1);
        } catch (GoogleAuthException e) {
            Log.e("GOOGLE+", e.getMessage());
        }
        return token;
    }

    @Override
    protected void onPostExecute(String s) {
        Log.d("TOKEN", "token: " + s);
    }
}

private void revokeGplusAccess() {
    if (mGoogleApiClient.isConnected()) {
        Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
        Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient)
        .setResultCallback(new ResultCallback<Status>() {
            @Override
            public void onResult(Status arg0) {
                Log.e("GOOGLE+", "User access revoked!");
                mGoogleApiClient.connect();
            }

        });
    }
}

这段代码在Activity中编写时有效,但是当我将其切换为Fragment时,则会出现无限循环.
我做错了什么?是否有其他机构可以使用Google以及使用访问令牌登录Google的其他示例?

谢谢,
宜兰

解决方法:

当您在onStop()中断开googleApiClient时,就会出现无限循环问题.您应该只在onDestroy()中执行此操作.

希望能帮助到你.

标签:android-fragments,google-play-services,google-plus,android
来源: https://codeday.me/bug/20191029/1958392.html