简体   繁体   中英

How to know if user decline signing-in from Google Play Games Services?

According to Google Quality Checklist for Google Play Games Services the app must try to " Automatically prompt the player to sign in when your game launches " and " Remember if players declined signing-in. " and do not let the app to keep asking the user every time it start. So, what is the event for this? there is not any reference on google documentation about this.

Please note before vote this question duplicated, that I am not asking about how to know if the user has logged out. What I want to do is to get the user answer from very first request in case is declined and keep it.

A bit late, I don't know if you solved it but I just faced the same problem. And here is how I fixed it.

I have a base Activity to handle Google Play Game login and I put this code in it

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RC_SIGN_IN) {
        mResolvingConnectionFailure = false;
        if (resultCode == RESULT_OK) {
            googleApiClient.connect();
        } else if (resultCode == RESULT_CANCELED) {
            /*
            * Put a boolean in your SharedPreferences 
            */
        } else {
            BaseGameUtils.showActivityResultError(this, requestCode, resultCode, R.string.signin_other_error);
        }
    }
}

According to your link, the best practice for remembering if a player declined to sign in:

1.5 Best practice Remember if players declined signing-in.

If the player declines to sign in when your game initially starts the sign-in flow (for example, if they clicked Cancel in the sign-in UI ), you should still allow the player to proceed with gameplay. When the player launches your game again, don't invoke the sign-in flow automatically. This saves players from having to repeatedly decline signing in whenever they start your game.

One exception is if players are trying to access a gameplay feature that is dependent on being signed-in (for example, starting a multiplayer match). In this case, prompt them to sign in before continuing with gameplay.

The bolded phrase there is a big hint on what to do. I can show you one way to ask a user to sign-in to Google:

new AlertDialog.Builder(YourActivity.this)
            .setTitle("Sign-in to Google")
            .setMessage("Please sign-in to Google services")
            .setPositiveButton(android.R.string.sign_in, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    //Sign in the user

                }
            })
            .setNegativeButton(android.R.string.cancel_sign_in, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    // This is where you want to remember their choice:
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putBoolean("declined", true);
                    editor.commit();
                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();

Now when you start your app you call to the SharedPreferences and check the boolean that was saved, and if "declined" = true then you do not show the alertDialog again. Like this:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SharedPreferences prefs = getSharedPreferences("MyAppPrefs", MODE_PRIVATE);
    Boolean declined = prefs.getBoolean("declined", false);
    if(!declined){
        //show dialog again
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM