Google Plus GoogleApiClient connect () without account selection dialog
I am working on enabling Google+ login in the app and now it works for me mostly. Following the instructions here:
I was able to add a Sign In to Google button, integrate
from the Services Services, and allow the user to sign in to the Google account of their choice.
However, in order to comply with Google's terms of service, I also want to provide a way (in a different form) to completely disable their Google+ account from the app. According to google documentation here:
It's a simple call to call
. The problem is that the first thing to do is tie
The process of getting such an object is described in the first link: use
to create a client object, call
on it and use the appropriate callbacks to detect connection success or failure. But if the user has multiple Google accounts on their phone, then it
crashes immediately, requiring me to invoke a permission intent, which will pop up a dialog asking them to choose which account they want to use.
But it won't work for this case because they are already signed in to Google+, so I should already know which account to use. Remember, at this point, all I have to do is create an object
signed into the account they were logged into earlier, so I can pass it to
. But I don't know how to do it.
I went through the documentation and searched every combination of words I can think of to explain this problem, but I haven't found anything yet. Is there generally a way to create an instance
with a specific account rather than requiring the user to select from a list of existing accounts? Or, if not, is there any information stored in the Preferences that I can get to get the token of the account they have already allowed in my application?
source to share
Of course, as soon as I post this, I stumble upon the answer.
Any curiosity, this is how you do it: you just need to add one more step to the GoogleApiClient builder; call the method
to specify which account you want to build the api client object to. So:
mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(Plus.API) .addScope(Plus.SCOPE_PLUS_PROFILE) .build();
Becomes as follows:
mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(Plus.API) .addScope(Plus.SCOPE_PLUS_PROFILE) .setAccountName(<gmail address>) .build();
And then it works.
source to share