从Google+注销其他活动 [英] logout from Google+ from other activity

查看:200
本文介绍了从Google+注销其他活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近实施了Google+ API。我设法成功认证并进入主要活动。我的问题是,我想在操作栏菜单中包含一个LogOut选项,当用户回来时,系统会提示他再次登录。



我阅读几个答案,但我无法实现它们。



您能否建议实施此解决方案的最佳方法并提出一个很好的示例?

谢谢,<因此,我设法解决了这个问题,我创建了下面的基类,并将它继承了所需的活动,我测试了它按预期工作。对于需要构建它的想法的任何人,您可以使用下面的代码。



在主要登录活动中,需要根据两点来更改flags状态点击事件。



如果您想从其他活动的G +帐户注销,请务必首先初始化GoogleApiClient,否则您将收到空对象引用异常。 / p>

希望能帮助别人......

  public class BaseClass extends AppCompatActivity实现
GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {

/ *用于调用用户交互登录的请求代码。 * /
private static final int RC_SIGN_IN = 0;

保护GoogleApiClient mGoogleApiClient;
保护上下文mContext;

/ *mIntentInProgress是一个标志,指示PendingIntent正在进行中,并阻止
* us启动其他意图。
*如果我们正在解析ConnectionResult的过程中,则为true
*mSignInClicked如果单击登录按钮,则FLAG为True。如果属实,我们知道要解决所有
*问题而无需等待登录。
* /

public boolean mIntentInProgress;
public boolean mSignInClicked;

public String mPersonName;
public String mImageUrl;
public String mEmailAddress;

public BaseClass(){

}



public void CreateClient(Context mContext){
//返回GoogleAPI客户端的客户端构建器,建立从应用到G +服务的连接
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)//添加OnConnected和OnConnectionSuspended方法来控制连接状态
.addOnConnectionFailedListener(this)//在连接失败的情况下添加OnConnectionFaild监听器。
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_PROFILE)//用户的配置文件许可
.addScope(Plus.SCOPE_PLUS_LOGIN)//从用户登录详细信息
.build();
mGoogleApiClient.connect();


$ b @Override
public void onConnected(Bundle bundle){
Log.i(google基类,onConnected invoked) ;
如果(Plus.PeopleApi.getCurrentPerson(mGoogleApiClient)!= null){
人{b $ b $ Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
mPersonName = currentPerson.getDisplayName();
mImageUrl = currentPerson.getImage()。getUrl();
mEmailAddress = Plus.AccountApi.getAccountName(mGoogleApiClient);
}
} catch(Exception e){
e.printStackTrace();
}}

@Override
public void onConnectionSuspended(int i){
Log.i(google base class,onConnectionSuspended invoked);
mGoogleApiClient.connect();



@Override
public void onConnectionFailed(ConnectionResult result){

Log.i(google base class, onConnectionFailed调用);

if(!mIntentInProgress){
if(mSignInClicked&& result.hasResolution()){
//用户已经点击'登录',所以我们尝试解决所有
//错误,直到用户登录,或者他们取消。
尝试{
result.startResolutionForResult(this,RC_SIGN_IN);
mIntentInProgress = true;
} catch(IntentSender.SendIntentException e){
//意图在发送之前被取消。返回到默认的
//状态并尝试连接以获取更新的ConnectionResult。
mIntentInProgress = false;
mGoogleApiClient.connect();





$ b public void onLogout(){

Log.i( 基类,注销调用);
if(mGoogleApiClient.isConnected()){
Log.i(base class,logout invoked);
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();




$ b / **
*撤销谷歌存取
* * /
public 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(base class,User access revoked!);
mGoogleApiClient.connect();

}

});
}
}
}


I have recently implemented the Google+ API. I managed to succesfully authenticated and move forward to the main activity. My issue is that I would like to include a "LogOut" option in the action bar menu and when the user comes back he will be prompted to login again.

I read several answers however I wasnt able to implement them.

can you please suggest the best way to implement this solution and suggest a good example?

Thanks,

解决方案

So I manage to solve the issue, i created the below base class and just inherited it over the needed activities, I tested and it worked as expected. to anyone who needs an idea on how to build it you can use the below code.

Two points, on the main login activity you will need to change the flags state according to the on click events.

if you like to logout from G+ account on other activity, make sure to initilize the GoogleApiClient first otherwise you will get null object reference exception.

hope that will help someone...

public class BaseClass extends AppCompatActivity  implements 
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{

    /* Request code used to invoke sign in user interactions. */
    private static final int RC_SIGN_IN = 0;

    protected GoogleApiClient mGoogleApiClient;
    protected Context mContext;

    /* "mIntentInProgress" is A flag indicating that a PendingIntent is in progress and prevents
     * us from starting further intents.
     * True if we are in the process of resolving a ConnectionResult
     * "mSignInClicked" FLAG is True if the sign-in button was clicked.  When true, we know to resolve all
     * issues preventing sign-in without waiting.
     */

    public boolean mIntentInProgress;
    public boolean mSignInClicked;

    public String mPersonName;
    public String mImageUrl;
    public String mEmailAddress;

    public BaseClass(){

    }



     public void CreateClient(Context mContext){
        //Client builder that return GoogleAPI client, make the connection from the app to the G+ service
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)//add OnConnected and OnConnectionSuspended methods to control the connection state
                .addOnConnectionFailedListener(this) // add OnConnectionFaild listener in case connection was failed.
                .addApi(Plus.API)
                .addScope(Plus.SCOPE_PLUS_PROFILE)//profile permission from the user
                .addScope(Plus.SCOPE_PLUS_LOGIN)// login details from the user
                .build();
         mGoogleApiClient.connect();
     }


    @Override
    public void onConnected(Bundle bundle) {
        Log.i("google base class", "onConnected invoked");
        try {
            if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
                mPersonName= currentPerson.getDisplayName();
                mImageUrl=currentPerson.getImage().getUrl();
                mEmailAddress = Plus.AccountApi.getAccountName(mGoogleApiClient);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.i("google base class", "onConnectionSuspended invoked");
        mGoogleApiClient.connect();
    }


    @Override
    public void onConnectionFailed(ConnectionResult result) {

        Log.i("google base class", "onConnectionFailed invoked");

        if (!mIntentInProgress) {
            if (mSignInClicked && result.hasResolution()) {
                // The user has already clicked 'sign-in' so we attempt to resolve all
                // errors until the user is signed in, or they cancel.
                try {
                    result.startResolutionForResult(this, RC_SIGN_IN);
                    mIntentInProgress = true;
                } catch (IntentSender.SendIntentException e) {
                    // The intent was canceled before it was sent.  Return to the default
                    // state and attempt to connect to get an updated ConnectionResult.
                    mIntentInProgress = false;
                    mGoogleApiClient.connect();
                }
            }
        }

    }

    public void onLogout() {

        Log.i("base class", "logout invoked");
        if (mGoogleApiClient.isConnected()) {
            Log.i("base class", "logout invoked");
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            mGoogleApiClient.disconnect();
            mGoogleApiClient.connect();

        }
    }


    /**
     * Revoking access from google
     * */
    public 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("base class", "User access revoked!");
                            mGoogleApiClient.connect();

                        }

                    });
        }
    }
}

这篇关于从Google+注销其他活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆