如何从谷歌加帐户注册google plus在android中集成

时间:2014-10-18 07:42:47

标签: android google-plus logout

我做了以下工作,但在退出后无法再次登录应用程序

private void signOutFromGplus() {

    if (mGoogleApiClient.isConnected()) {

        // clearCookies();

        Plus.AccountApi
                .clearDefaultAccount(mGoogleApiClient);

        .mGoogleApiClient.disconnect();

        .mGoogleApiClient.connect();
    }
}

5 个答案:

答案 0 :(得分:2)

在此类中为您分配变量(使用此类的优点是此类在启动应用程序时启动,您可以在应用程序的任何位置访问此类): 的 MyApplication.class

import android.app.Application;
import android.support.v7.app.AppCompatActivity;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.GoogleApiClient;
public class MyApplication extends Application {
private GoogleApiClient mGoogleApiClient;
private GoogleSignInOptions gso;
public AppCompatActivity activity;
public GoogleSignInOptions getGoogleSignInOptions(){
    gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();
    return gso;
}
public GoogleApiClient getGoogleApiClient(AppCompatActivity activity, GoogleApiClient.OnConnectionFailedListener listener){
    this.activity = activity;
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this.activity, listener)
            .addApi(Auth.GOOGLE_SIGN_IN_API, getGoogleSignInOptions())
            .build();
    return mGoogleApiClient;
}
}

登录活动

    @Override
protected void onStart() {
    mGoogleApiClient.connect();
    super.onStart();
    OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
    if (opr.isDone()) {
        Log.d("TAG", "Got cached sign-in");
        GoogleSignInResult result = opr.get();
        handleSignInResult(result);
    } else {
        opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
            @Override
            public void onResult(GoogleSignInResult googleSignInResult) {
                //hideProgressDialog();
                handleSignInResult(googleSignInResult);
            }
        });
    }
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    gso = ((MyApplication) getApplication()).getGoogleSignInOptions();
    mGoogleApiClient = ((MyApplication) getApplication()).getGoogleApiClient(LoginActivity.this, this);
}
@Override
protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

在您要退出的第二个活动中

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    mGoogleApiClient = ((MyApplication) getApplication()).getGoogleApiClient(HomeActivity.this, this);

btnLogout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
                    new ResultCallback<Status>() {
                        @Override
                        public void onResult(Status status) {
                            Intent i = new Intent(HomeActivity.this, LoginActivity.class);
                            startActivity(i);
                            finish();
                        }
                    });
        }
    });
}

在您的应用级build.gradle文件中,将Google Play服务声明为依赖项:

dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.google.gms:google-services:1.5.0-beta2'

}

让MyApplication类扩展默认的Application类。另外,打开Manifest文件并在application元素中添加这行代码。

android:name=".MyApplication"

答案 1 :(得分:1)

这可能是一个老问题,但我想在这里清楚,很可能是第一次登录谷歌的新人。在调用以下方法后,他们可能在自动登录Google帐户时遇到问题。

private void signOutFromGplus() {

    if (mGoogleApiClient.isConnected()) {

        // clearCookies();

        Plus.AccountApi
                .clearDefaultAccount(mGoogleApiClient);

        .mGoogleApiClient.disconnect();

        .mGoogleApiClient.connect();
    }
}

解决方案:确保您的mGoogleApiClient尚未在某处断开连接。那么为什么它没有进入这个if循环的原因。

最有可能在onStop()onDestroy()

答案 2 :(得分:0)

Sign-outRevoke之间存在差异。您似乎想要断开用户与应用程序的连接,因此下次再次提示用户登录。如果是这种情况,请尝试以下操作(有关详细信息,请参阅Google+ Sign-in for Android):

// Prior to disconnecting, run clearDefaultAccount().
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient)
    .setResultCallback(new ResultCallback<Status>() {

  onResult(Status status) {
    // mGoogleApiClient is now disconnected and access has been revoked.
    // Trigger app logic to comply with the developer policies
  }

});

答案 3 :(得分:0)

虽然Google的说明在断开连接()后立即调用connect(),但我遇到了与您相同的问题 - 用户再次登录而没有任何互动。

最后,我完全忽略了指令,并从我的代码中删除了对connect()的调用。适合我。

答案 4 :(得分:0)

private void googlePlusLogout() {

        if (mGoogleApiClient.isConnected()) {    
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);    
            mGoogleApiClient.disconnect();    
            mGoogleApiClient.connect();    
        }    
    }

尝试按下按钮调用此方法。