Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make the Play Game Services not automatically sign in at the startup?

Google provides the BaseGameUtils library, and recommend us to extends its BaseGameActivity. However, this class makes the game automatically sign in whenever the game is started. If the player does not want to or cannot connect to his Google account, this can be very time consuming at the beginning of the game.

So I dont' want this feature. Instead, I want to provide a sign in button. The player is connected only when he click that button. And from that point on, every time the player starts the game, he is automatically connected to his Google account without clicking any button. How can I do this?

like image 683
Hải Phong Avatar asked Feb 26 '14 15:02

Hải Phong


People also ask

How do I disable auto sign in on play games?

Open the Google Play Games app on your Android device. Tap the settings (three dots) in the top navigation bar. Turn off Sign in to games automatically.

How do you sign out of Google Play Games account?

Sign out of Google Play Games ServiceOpen the Options menu. In the Options menu, click the Sign Out button.

What is play games account?

Play Games Services sign-in provides you with a player's gaming identity, which is a platform-level, gaming-specific identity for Android players. This identity helps build a relationship between your game and the player. Players are more willing to use this identity to sign in than with alternate centralized systems.


2 Answers

OK, I have figured it out, by default, the maximum auto sign-in times is 3, which means if the user cancels 3 times, then the app will never again (unless you clear the app's data) automatically sign in. It's stored in GameHelper.java

 // Should we start the flow to sign the user in automatically on startup? If so, up to  // how many times in the life of the application?  static final int DEFAULT_MAX_SIGN_IN_ATTEMPTS = 3;  int mMaxAutoSignInAttempts = DEFAULT_MAX_SIGN_IN_ATTEMPTS; 

And it also provides a function to set this maximum number

public void setMaxAutoSignInAttempts(int max) {         mMaxAutoSignInAttempts = max; } 

So if you don't want any automatic signing-in attempt at all, just call this function

This is if you don't want to extends BaseGameActivity

gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES); gameHelper.enableDebugLog(true); gameHelper.setup(this); gameHelper.setMaxAutoSignInAttempts(0); 

Or if you extends BaseGameActivity

getGameHelper().setMaxAutoSignInAttempts(0); 
like image 110
Hải Phong Avatar answered Sep 23 '22 17:09

Hải Phong


In the GameHelper.java file there is a boolean attribute called mConnectOnStart that by default it is set to true. Just change it to false instead:

boolean mConnectOnStart = false; 

Additionally, there is a method provided for managing this attribute from the outside of the class:

// Not recommended for general use. This method forces the "connect on start" // flag to a given state. This may be useful when using GameHelper in a  // non-standard sign-in flow. public void setConnectOnStart(boolean connectOnStart) {     debugLog("Forcing mConnectOnStart=" + connectOnStart);     mConnectOnStart = connectOnStart; } 

You can use the method above in order to customize your sign in process. In my case, similar to you, I don't want to auto connect the very first time. But if the user was signed in before, I do want to auto connect. To make this possible, I changed the getGameHelper() method that is located in the BaseGameActivity class to this:

public GameHelper getGameHelper() {     if (mHelper == null) {         mHelper = new GameHelper(this, mRequestedClients);         mHelper.enableDebugLog(mDebugLog);          googlePlaySharedPref = getSharedPreferences("GOOGLE_PLAY",                 Context.MODE_PRIVATE);         boolean wasSignedIn = googlePlaySharedPref.getBoolean("WAS_SIGNED_IN", false);         mHelper.setConnectOnStart(wasSignedIn);     }     return mHelper; } 

Every time, getGameHelper() method is called from onStart() in BaseGameActivity. In the code above, I just added the shared preference to keep if the user was signed in before. And called the setConnectOnStart() method according to that case.

Finally, don't forget to set the "WAS_SIGNED_IN" (or something else if you defined with different name) shared preference to true after user initiated sign in process. You can do this in the onSignInSucceeded() method in the BaseGameActivity class.

Hope this will help you. Good luck.

like image 40
osmangokalp Avatar answered Sep 24 '22 17:09

osmangokalp