问题
I'm developing and Android app with Google Play Games Services and I've followed the getting started guide by importing the BaseGameUtils as a module into my IntelliJ project. This is now working fine except that whenever I open an activity which inherits BaseGameActivity
and call beginUserInitiatedSignIn()
, the user is prompted to select which Google account to connect with (if more than one is present at the phone). Now this would be fine if it weren't for the fact that in my app, the user is already authed through Google Play Services using OAuth2 (GoogleAuthUtil/AccountPicker). It's given which account he/she wants to use - one shouldn't need to ask again.
Is there anyway I can modifiy BaseGameActivity.java
or GameHelper.java
so that the user is not prompted again? And/or do i need to alter how I log in with OAuth2?
I've tried adding the four scopes PLUS_PROFILE, PLUS_LOGIN, GAMES and APP_STATE to my OAuth login routine, but the AccountPicker-popup sill appears in my activity inheriting from BaseGameActivity.
Update: Logcat-log
07-23 11:45:25.275: DEBUG/CSDN-GMS(22315): isGooglePlayServicesAvailable returned 0
07-23 11:45:25.275: DEBUG/CSDN-GMS(22315): beginUserInitiatedSignIn: starting new sign-in flow.
07-23 11:45:25.285: DEBUG/CSDN-GMS(22315): Connecting GamesClient.
07-23 11:45:25.295: DEBUG/CSDN-GMS(22315): onStart.
07-23 11:45:25.295: DEBUG/CSDN-GMS(22315): onStart: connecting clients.
07-23 11:45:25.295: DEBUG/CSDN-GMS(22315): Connecting GamesClient.
07-23 11:45:25.395: DEBUG/CSDN-GMS(22315): onConnectionFailed: result 4
07-23 11:45:25.395: DEBUG/CSDN-GMS(22315): onConnectionFailed: since user initiated sign-in, trying to resolve problem.
07-23 11:45:25.395: DEBUG/CSDN-GMS(22315): resolveConnectionResult: trying to resolve result: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{4190b780: android.os.BinderProxy@4190a780}}
07-23 11:45:25.395: DEBUG/CSDN-GMS(22315): result has resolution. Starting it.
回答1:
Do you need to login with OAuth2 first, or could you sign into Google Games first? If you can get away with signing into Google Games first, you could maybe use GamesClient.getCurrentAccountName()
. From https://developer.android.com/reference/com/google/android/gms/games/GamesClient.html#getCurrentAccountName() :
Get the name of the currently selected account. This is the account the user has chosen to use for Google Play Games.
Then you could feed that account selection into OAuth2 authentication.
回答2:
I suggest you look at / post a logcat trace. This is what a succesful trace looks like when the user is not signed on :
07-04 10:21:54.511: D/ian_(1781): MultiTab3 beginUserInitiatedSignIn
07-04 10:21:54.531: D/ian_(1781): isGooglePlayServicesAvailable returned 0
07-04 10:21:54.531: D/ian_(1781): beginUserInitiatedSignIn: continuing pending sign-in flow.
07-04 10:21:54.611: D/ian_(1781): resolveConnectionResult: trying to resolve result: C onnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{40f3ed38: android.os.BinderProxy@40ee3de0}}
07-04 10:21:54.611: D/ian_(1781): result has resolution. Starting it.
07-04 10:21:54.621: D/ian_(1781): startResolutionForResult - this may be prob ?
07-04 10:23:29.480: D/ian_(1781): MultiPlayer onActivityResult called9001-1null
07-04 10:23:29.520: D/ian_(1781): MultiPlayer passing onActivityResult to MultiTab3 Req/Resp/Data=9001-1null
07-04 10:23:29.520: D/ian_(1781): MultiTab3 onActivityResult - passing through to GameHelper ...9001-1null
07-04 10:23:29.520: D/ian_(1781): onActivityResult, req 9001 response -1
07-04 10:23:29.520: D/ian_(1781): responseCode == RESULT_OK. So connecting.
07-04 10:23:30.130: D/ian_(1781): onConnected: connected! client=1
07-04 10:23:30.130: D/ian_(1781): All clients now connected. Sign-in successful.
07-04 10:23:30.130: D/ian_(1781): All requested clients connected. Sign-in succeeded!
beginUserInitiatedSignIn only launches the Google "choose an account" dialog (result has resolution. Starting it) if the user is not signed in (or if there was some problem). You will see that in my example the reason this happened was SIGN_IN_REQUIRED.
Look at your logcat and see what the message says. If necessary you can get a more detailed trace.
N.B. If the user is signed in then the path is shorter and simpler and the "choose an account" dialog is not displayed - perhaps it would be a good idea to try it out on one of the sample apps first ?
来源:https://stackoverflow.com/questions/17701504/how-can-i-avoid-user-being-prompted-a-second-time-for-google-account-selection