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

前端 未结 5 1635
予麋鹿
予麋鹿 2020-12-30 20:59

Google provides the BaseGameUtils library, and recommend us to extends its BaseGameActivity. However, this class makes the game automatically sign in whenever t

5条回答
  •  太阳男子
    2020-12-30 21:54

    Actually, the code from Google works exactly like you are talking about in the second paragraph.

    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?

    1. The Helper.setup method creates the clients

    2. onStart looks at an internal boolean for auto-sign-in. If the user was previously connected (and user did not sign out, or there was no error in disconnecting) then it will try to re-establish sign in.

    3. beginUserInitiatedSignIn will set the auto-sign-in boolean if a successful connection is initiated

    4. onStop will only terminate the connections gracefully, it does not reset the boolean

    So the only way that the user sees the sign in process when your app starts, is if beginUserInitiatedSignIn is somehow called before pushing a button.

    Make sure your beginUserInitiatedSignIn is not in your onStart method, nor is it called except by any other means than when your sign-in button is clicked and the User is NOT signed in.

     @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);
        mHelper = new GameHelper(this);
        if (mDebugLog) {
            mHelper.enableDebugLog(mDebugLog, mDebugTag);
        }
        mHelper.setup(this, mRequestedClients, mAdditionalScopes);
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        mHelper.onStart(this);
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        mHelper.onStop();
    }
    
    
    protected void beginUserInitiatedSignIn() {
        mHelper.beginUserInitiatedSignIn();
    }
    

    From the BaseGameUtil class

    /** * Example base class for games. This implementation takes care of setting up * the GamesClient object and managing its lifecycle. Subclasses only need to * override the @link{#onSignInSucceeded} and @link{#onSignInFailed} abstract * methods. To initiate the sign-in flow when the user clicks the sign-in * button, subclasses should call @link{#beginUserInitiatedSignIn}. By default, * this class only instantiates the GamesClient object. If the PlusClient or * AppStateClient objects are also wanted, call the BaseGameActivity(int) * constructor and specify the requested clients. For example, to request * PlusClient and GamesClient, use BaseGameActivity(CLIENT_GAMES | CLIENT_PLUS). * To request all available clients, use BaseGameActivity(CLIENT_ALL). * Alternatively, you can also specify the requested clients via * @link{#setRequestedClients}, but you must do so before @link{#onCreate} * gets called, otherwise the call will have no effect. * * @author Bruno Oliveira (Google)

提交回复
热议问题