2

I would like to know if there is an account available on an Android device which is registered as a Google Play Games account.

I am using Java, and LibGDX.

Any ideas?

Z0q
  • 1,689
  • 3
  • 28
  • 57

1 Answers1

1

Edit: Google Play Games no longer needs a Google+ account as mentioned here

Issue: Asking for unnecessary scopes

 // Don’t do it this way!  
 GoogleApiClient gac = new GoogleApiClient.Builder(this, this, this)  
           .addApi(Games.API)  
           .addScope(Plus.SCOPE_PLUS_LOGIN) // The bad part  
           .build();  
 // Don’t do it this way!  

In this case, the developer is specifically requesting the plus.login scope. If you ask for plus.login, your users will get a consent dialog.

Solution: Ask only for the scopes you need

// This way you won’t get a consent screen  
 GoogleApiClient gac = new GoogleApiClient.Builder(this, this, this)  
           .addApi(Games.API)  
           .build();  
 // This way you won’t get a consent screen  

Remove any unneeded scopes from your GoogleApiClient construction along with any APIs you no longer use.


Original answer:

A Google Play Games account is available when the user is signed in with a Google+ account as mentioned here and here, and that by itself is available if the user is signed in with a Google account, so our first approach would be:

  1. Check if there is a Google Account logged-in.
  2. Check if that user is logged-in Google+.

To check if the Android device has a Google user logged-in:

public boolean isLoggedInGoogle() {
    AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
    Account[] list = manager.getAccounts();

    for (Account account : list) {
        if (account.type.equalsIgnoreCase("com.google")) {
            return true;
        }
    }

    return false;
}

Now, to check if the Android device has a Google+ user logged-in.

You can try to start a login sequence with a Google API client, as mentioned in this question:

private GoogleApiClient buildGoogleApiClient() {
    return new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API, null)
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .build();
}

You can also just make use of the BaseGameUtils of the Play Games Services APIs. There are a few samples of how to achieve it in this link.

Community
  • 1
  • 1
Evin1_
  • 12,292
  • 9
  • 45
  • 47
  • Thank you very much for the detailed answer. I do use BaseGameUtils. But what happens if the user does have Google+, but is not yet signed in? And I have a second question: Would it make sense not to start Google Play Games when a user doesn't have a Google+ account yet? I think it might scare away users who prefer not to use Google+. – Z0q Feb 10 '16 at 11:04
  • @Z0q Those were very good follow up questions! So I found an Android Developer post that said Google+ weren't needed anymore. I edited my answer already with the info. http://android-developers.blogspot.com/2016/01/play-games-permissions-are-changing-in.html – Evin1_ Feb 10 '16 at 16:36
  • @Z0q And... if you just want to remove the signing logic or modify, I'd recommend you to check this question: http://stackoverflow.com/questions/22046530/how-can-i-make-the-play-game-services-not-automatically-sign-in-at-the-startup – Evin1_ Feb 10 '16 at 16:37
  • 1
    Thank you. That clarifies a lot for me. However, my question is `I would like to know if there is an account available on an Android device which is registered as a Google Play Games account.`. You told me how to see if the account is a Google account, but how do I know if it is also registered for Google Play Games? – Z0q Feb 10 '16 at 17:28
  • 1
    Oh... you are right, I'm not even answering the original question with the edits. So... what I would do is: Try to login in the background: The blog post mentioned that "Players are prompted to sign-in once per account, rather than once per game". So if there is an account already logged in. You would receive something from the server. – Evin1_ Feb 10 '16 at 17:36
  • @Evin1_ will this solution work for my question? https://stackoverflow.com/questions/71151597/com-google-android-gms-common-api-apiexception-17-api-games-api-is-not-availa – AtomicallyBeyond Feb 18 '22 at 06:33