从Android应用程序中集成的Google Plus注销

比纳尔

我已按照以下教程将Google+集成到android应用中。https://developers.google.com/+/mobile/android/sign-in#add_the_google_sign-in_button_to_your_app http://www.riskcompletefailure.com/2013/03/common-problems-with-google-sign-in-on .html http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/

我可以登录Google帐户,也可以检索信息。事情是我无法注销。

我在登录活动中登录G +,并使用“共享首选项”存储会话,然后在另一个基本活动中关闭会话,将布尔值传递给有关会话关闭的登录活动。尽管会话未处于活动状态或用户尚未登录,但是只要启动登录活动,登录活动就会自动连接到G +。试图在onConnected上执行逻辑,但无济于事。

以下是我的代码段。

public class LoginActivity extends BaseActionBar implements OnClickListener,
            ConnectionCallbacks, OnConnectionFailedListener {

        private Button btnLogin, btnFgetPwrd, btnRegister;
            // Logcat tag
        private static final String TAG = "LoginActivity";
        // Google Plus
        private static final int GOOGLE_SIGN_IN = 0;
        // Google Plus Profile Data
        String GpersonName, GpersonPhotoUrl, Gemail, googleError, GCustId;
        // Google client to interact with Google API
        private GoogleApiClient mGoogleApiClient;
        private SignInButton btnGooglePlus;

        // A flag indicating that a PendingIntent is in progress and prevents us
        // from starting further intents.
        private boolean mIntentInProgress;
        // Track whether the sign-in button has been clicked so that we know to
        // resolve all issues preventing sign-in without waiting.
        private boolean mSignInClicked;
        private ConnectionResult mConnectionResult;

        // Session Manager Class
        SessionManager session;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login);

            // Initializing google plus api client
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this).addApi(Plus.API)
                    .addScope(Plus.SCOPE_PLUS_LOGIN).build();
            // Session Manager
            session = new SessionManager(getApplicationContext());

            if (session.isLoggedIn() == false) {
                Log.v(TAG, "false");
                mSignInClicked = false;
                DataStore.LoginGoogle = false;
                if (mGoogleApiClient.isConnected()) {
                    Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
                    mGoogleApiClient.disconnect();
                    mGoogleApiClient.connect();
                }
            } else {
                Intent i = new Intent(LoginActivity.this, UserProfileActivity.class);
                startActivity(i);
            }
            btnLogin.setOnClickListener(this);
            btnFgetPwrd.setOnClickListener(this);
            btnRegister.setOnClickListener(this);
            btnGooglePlus.setOnClickListener(this);
        }

        // Facebook and Google Plus
        @Override
        protected void onActivityResult(int requestCode, int responseCode,
                Intent intent) {
            if (requestCode == GOOGLE_SIGN_IN) {
                if (responseCode != RESULT_OK) {
                    mSignInClicked = false;
                }
                mIntentInProgress = false;
                if (!mGoogleApiClient.isConnecting()) {
                    mGoogleApiClient.connect();
                }
            } else if (requestCode == FB_SIGN_IN) {
                Session.getActiveSession().onActivityResult(this, requestCode,
                        responseCode, intent);
            }
        }
        // Google Plus
        protected void onStart() {
            super.onStart();
            mGoogleApiClient.connect();
        }

        protected void onStop() {
            super.onStop();
            if (mGoogleApiClient.isConnected()) {
                mGoogleApiClient.disconnect();
            }
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {

            case R.id.loginBtn:
                // Login Button Clicked
                break;

            case R.id.loginBtnFrgtPass:
                // Forgot Button Clicked
                i = new Intent(LoginActivity.this, ForgotPasswordActivity.class);
                startActivity(i);
                break;
            case R.id.loginBtnRegis:
                // Register Button Clicked
                i = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(i);
                break;

            case R.id.loginBtn_sign_in:
                signInWithGplus();
                break;
            }
        }

        // Sign-in into google
        private void signInWithGplus() {
            if (!mGoogleApiClient.isConnecting()) {
                mSignInClicked = true;
                resolveSignInError();
            }
        }

        // Method to resolve any sign-in errors
        private void resolveSignInError() {
            Log.v(TAG, mConnectionResult.toString());
            if (mConnectionResult.hasResolution()) {
                try {
                    mIntentInProgress = true;
                    startIntentSenderForResult(mConnectionResult.getResolution()
                            .getIntentSender(), GOOGLE_SIGN_IN, null, 0, 0, 0);
                    // mConnectionResult
                    // .startResolutionForResult(this, GOOGLE_SIGN_IN);
                } catch (SendIntentException e) {
                    mIntentInProgress = false;
                    mGoogleApiClient.connect();
                }
            }
        }

        // Google+ connection
        @Override
        public void onConnected(Bundle arg0) {
            // TODO Auto-generated method stub
            Log.v(TAG, "onConnected");

            mSignInClicked = false;
            Toast.makeText(this, "User is connected to Google+", Toast.LENGTH_LONG)
                    .show();

            btnLogin.setEnabled(false);
            // Get user's information
            getProfileInformation();

        }

        // Google+ connection
        @Override
        public void onConnectionSuspended(int arg0) {
            // TODO Auto-generated method stub
            mGoogleApiClient.connect();
        }

        // Google+ connection
        @Override
        public void onConnectionFailed(ConnectionResult result) {
            // TODO Auto-generated method stub

            Log.v(TAG, result.toString());
            if (!result.hasResolution()) {
                GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
                        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();
                }
            }
        } // Normal Logging in
    }

从基本活动注销会话的代码段

if (session.isLoggedIn()) {
                session.logoutUser();
                DataStore.LoginGoogle = false;
                setOptionTitle(R.id.action_login, "Login");
            }
比纳尔

我找到了解决方案。

在参考以下站点后,我找到了解决方案。

Android在mConnectionResult.hasResolution()上实现Google加登录错误

http://www.riskcompletefailure.com/2013/03/common-problems-with-google-sign-in-on.html

我必须在onConnected中检查日志记录会话,并执行注销过程。下面是代码片段。

// Google+ connection
@Override
public void onConnected(Bundle arg0) {
    // TODO Auto-generated method stub
    Log.v(TAG, "onConnected");
    if (ShopRunDataStore.LoginGoogle) {
        Log.v(TAG, "Google logged in");
        mSignInClicked = false;
        Toast.makeText(this, "User is connected to Google+",
                Toast.LENGTH_LONG).show();
        btnfacebook.setEnabled(false);
        btnLogin.setEnabled(false);
        // Get user's information
        getProfileInformation();
    } else {
        Log.v(TAG, "In if condition to log off");
        if (mGoogleApiClient.isConnected()) {
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            mGoogleApiClient.disconnect();
            // mGoogleApiClient.connect();
            mSignInClicked = false;
            btnGooglePlus.setEnabled(true);
            btnfacebook.setEnabled(false);
            btnLogin.setEnabled(true);
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从应用程序Android注销

来自分类Dev

如何在Java单击按钮中注销我的Android应用程序中的Google用户?

来自分类Dev

如何在Python Django应用程序中集成Paypal

来自分类Dev

在Symfony应用程序中集成PayPal IPN

来自分类Dev

如何在Android应用程序中集成TEST PayU Payment Gateway?

来自分类Dev

在Android应用程序中注销GoogleApiClient

来自分类Dev

如何在Android移动应用程序中集成opentok?

来自分类Dev

从Google登录集成android中注销

来自分类Dev

在Android应用程序中集成两个Unity项目

来自分类Dev

是否可以在C#窗口形式的桌面应用程序中集成Google Pay

来自分类Dev

如何从Skype应用程序注销?

来自分类Dev

如何通过Android应用程序在Google Plus中发布图片和链接网址?

来自分类Dev

Windows Form Desktop应用程序注销

来自分类Dev

如何在Phonegap for Android应用程序中集成Google Plus Oauth API

来自分类Dev

如何在Java中单击Button并在我的Android应用程序中注销Google用户?

来自分类Dev

MVC .NET应用程序启动注销

来自分类Dev

从多个应用程序注销

来自分类Dev

Web应用程序中的注销问题

来自分类Dev

我可以在Java应用程序中集成android代码吗?

来自分类Dev

从Android应用程序中共享Google Plus

来自分类Dev

在应用程序中集成MHTabBarController

来自分类Dev

在Android中集成Ola money api总是未安装应用程序?

来自分类Dev

Google Plus和Twitter应用程序在Android上的何处打开链接

来自分类Dev

使用NSTimer注销应用程序

来自分类Dev

在Django应用程序中集成Google登录和日历访问

来自分类Dev

从 Google Plus 注销/断开连接 (Ionic/Cordova -cordova-plugin-googleplus)

来自分类Dev

如何在我的应用程序中集成 google 登录和 firebase?

来自分类Dev

如何在 React Native 中集成 google pay(印度的支付应用程序)?

来自分类Dev

如何在 Android 应用程序中集成搜索功能

Related 相关文章

  1. 1

    从应用程序Android注销

  2. 2

    如何在Java单击按钮中注销我的Android应用程序中的Google用户?

  3. 3

    如何在Python Django应用程序中集成Paypal

  4. 4

    在Symfony应用程序中集成PayPal IPN

  5. 5

    如何在Android应用程序中集成TEST PayU Payment Gateway?

  6. 6

    在Android应用程序中注销GoogleApiClient

  7. 7

    如何在Android移动应用程序中集成opentok?

  8. 8

    从Google登录集成android中注销

  9. 9

    在Android应用程序中集成两个Unity项目

  10. 10

    是否可以在C#窗口形式的桌面应用程序中集成Google Pay

  11. 11

    如何从Skype应用程序注销?

  12. 12

    如何通过Android应用程序在Google Plus中发布图片和链接网址?

  13. 13

    Windows Form Desktop应用程序注销

  14. 14

    如何在Phonegap for Android应用程序中集成Google Plus Oauth API

  15. 15

    如何在Java中单击Button并在我的Android应用程序中注销Google用户?

  16. 16

    MVC .NET应用程序启动注销

  17. 17

    从多个应用程序注销

  18. 18

    Web应用程序中的注销问题

  19. 19

    我可以在Java应用程序中集成android代码吗?

  20. 20

    从Android应用程序中共享Google Plus

  21. 21

    在应用程序中集成MHTabBarController

  22. 22

    在Android中集成Ola money api总是未安装应用程序?

  23. 23

    Google Plus和Twitter应用程序在Android上的何处打开链接

  24. 24

    使用NSTimer注销应用程序

  25. 25

    在Django应用程序中集成Google登录和日历访问

  26. 26

    从 Google Plus 注销/断开连接 (Ionic/Cordova -cordova-plugin-googleplus)

  27. 27

    如何在我的应用程序中集成 google 登录和 firebase?

  28. 28

    如何在 React Native 中集成 google pay(印度的支付应用程序)?

  29. 29

    如何在 Android 应用程序中集成搜索功能

热门标签

归档