How can i check if user sign's out from games services default view?

随声附和 提交于 2019-11-29 10:56:36

问题


I integrated google games services in my game, including Leaderboards and achievements. If the user opens the leaderboard or achievement activity, he has the possibility to sign out from the settings in the right upper corner.

How can I check if the user is actually signed in? getGamesClient.isConnected() is still true, although the user logged out from the google view.

If I'm clicking the logout button (which is still there, becaus gamesClient is still connected) I get an SecurityException:

08-16 11:01:21.262 14288-14288/? E/AndroidRuntime: FATAL EXCEPTION: main java.lang.SecurityException at android.os.Parcel.readException(Parcel.java:1425) at android.os.Parcel.readException(Parcel.java:1379) at com.google.android.gms.internal.bm$a$a.a(Unknown Source) at com.google.android.gms.internal.bj.signOut(Unknown Source) at com.google.android.gms.games.GamesClient.signOut(Unknown Source)

At the moment, I am checking the ActivityForResult response code and disconnecting the GamesClient, if it's in inconsistent state, but I don't like that approach.


回答1:


Try handling onActivityResult:

public boolean onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == RC_YOUR_UNIQUE_ID 
            && resultCode == GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED) {
        mHelper.disconnect();
        // update your logic here (show login btn, hide logout btn).            
    } else {
        mHelper.onActivityResult(requestCode, resultCode, data);
    }
    return false;
}

RC_YOUR_UNIQUE_ID is id you've used for showing Leaderboard or Achievements activity.




回答2:


I think what you need is: isSignedIn(); Like this:

    public boolean getSignedIn() {
    return isSignedIn();
}

This is will return a true or false if user is signed or not. The method in GameHelper.java:

    /** Returns whether or not the user is signed in. */
public boolean isSignedIn() {
    return mState == STATE_CONNECTED;
}

Hope it helps.




回答3:


In my case I am doing as following. On MainActivity ovveride onActivityResult(). RC_UNUSED is the requestCode when you call to open Leaderboards, Achievements and Settings activity from Google Play service app.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == RC_UNUSED 
&& resultCode == GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED) {
        signOut();
    }

    Log.i("test", "On Activity result called");
}   

Write your signOut() method as following

public void signOut() {
    try {
        Games.signOut(mGoogleApiClient);
    }catch (SecurityException se){
        Log.i("test", "mGoogleApiClient status was disconnected when callin signOut status. message = " + se.getMessage());
    }
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
    // update your UI logic here (show login btn, hide logout btn).
}

Don't forget to update your UI on the end of signOut()



来源:https://stackoverflow.com/questions/18271874/how-can-i-check-if-user-signs-out-from-games-services-default-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!