如何在Android中的单独活动中从Google plus注销

时间:2015-08-28 11:34:19

标签: android google-api google-plus

我正在尝试将google plus登录与我的应用程序集成,我部分成功了。我能够使用google plus登录,并且我能够获取登录用户的电子邮件ID。对我来说唯一的问题现在我需要从googleplus实施注销,以便应用程序将重定向到登录页面,它应该显示弹出窗口,以便在用户单击登录按钮时选择要登录的ID。我已经完成了很多例子,但总的来说他们正在同一个活动中实现登录。我对这个问题已经坚持了近一个星期。请告诉我如何实现上述目标。 https://developers.google.com/+/mobile/android/sign-in这是我为登录集成所遵循的教程

这是我在主要活动中的代码

SharedPreferences shared = getSharedPreferences("MyPrefs", MODE_PRIVATE);
        String channel = (shared.getString("logedin", ""));
        Log.d("loggedin",channel);
        if(channel.equals("false")){
            revokeGplusAccess();
        }

这是我的revokeGplusAccess函数

private void revokeGplusAccess() {

            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient)
                    .setResultCallback(new ResultCallback<Status>() {
                        @Override
                        public void onResult(Status arg0) {
                            Log.e(TAG, "User access revoked!");
                            mGoogleApiClient.connect();
                            updateUI(false);
                        }

                    });
        }

以下是我的第二个活动的代码,其中有注销按钮

sharedpreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedpreferences.edit();

        editor.putString("logedin", "false");

        editor.commit();
        Intent intent = new Intent(this, MainActivity.class);

        startActivity(intent);

以下是logcat错误

java.lang.IllegalStateException: GoogleApiClient must be connected.
            at com.google.android.gms.common.internal.zzu.zza(Unknown Source)
            at com.google.android.gms.plus.Plus.zzf(Unknown Source)
            at com.google.android.gms.internal.zzpa.clearDefaultAccount(Unknown Source)
            at com.example.rating.Products.onSignedOut(Products.java:272)
            at com.example.rating.Products.onClick(Products.java:139)
            at android.view.View.performClick(View.java:4278)
            at android.view.View$PerformClick.run(View.java:17429)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5099)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

Try this. Hope it solve your problem.

// Google client to interact with Google API
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {

    ...........your code........

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

    ...........your code........

}


protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

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

/**
 * Sign-out from google method to be called in logout button click
 * */
private void signOutFromGplus() {
    if (mGoogleApiClient.isConnected()) {
        Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
        mGoogleApiClient.disconnect();
        mGoogleApiClient.connect();

        Intent i = new Intent(YourActivityName.this, YourLoginActivity.class);
        YourActivityName.this.startActivity(i);
        YourActivityName.this.finish();

    }
}


@Override
public void onConnectionSuspended(int arg0) {
    mGoogleApiClient.connect();
}