Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

android - How can I enforce GoogleApiClient to prompt account chooser UI each time I call connect?

Whenever I run the code for the very first time at my 1st app launched cycle

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(context)
    .addApi(Drive.API)
    .addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();

mGoogleApiClient.connect();

I can see the following account chooser.

enter image description here

However, if previous connect is success, and I run the same code again for first time at my 2nd app launched cycle.

The account chooser will not pop up again. GoogleApiClient is going to use the account name, I choose in previous app launch cycle.

I wish to have my account chooser pop up every-time.

I came across How to Clear GoogleApiClient Default Account and Credentials

The proposed solution doesn't work for my case.

mGoogleApiClient.clearDefaultAccountAndReconnect()

If I had been connected for my previous app cycle, and I call the above code first time in my current app cycle, I will get the following exception.

java.lang.IllegalStateException: GoogleApiClient is not connected yet.
    at com.google.android.gms.common.internal.zzx.zza(Unknown Source)
    at com.google.android.gms.common.api.internal.zzj.clearDefaultAccountAndReconnect(Unknown Source)

The following code won't work either.

if (mGoogleApiClient.isConnected()) {
    // No chance to execute this code, if you run this code during app launch.
    mGoogleApiClient.clearDefaultAccountAndReconnect();
} else {
    // No account chooser will pop up if you had been connected in previous app life cycle
    mGoogleApiClient.connect();
}

May I know, how can I enforce GoogleApiClient to prompt account chooser UI each time I call connect?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In both GDAA and the REST Api, you have two options:
1/ you do not specify the account and the underlying system will manage it.
2/ you manage the account yourself.

If you use the first approach, you will never know who the user of your app selected. You can only 'clean' the account by means of clearDefaultAccountAndReconnect. The selection dialog pops up again and the user can select (add) another account.

If you need to know the current selected user account (i.e. for caching /persistence), you must manage the account selection yourself as you can see here (for REST) or here (for GDAA) - just follow the REQ_ACCPICK trail and the UT.AM class. This way you'll be in full control.

So, the short answer to your question is that you pop the

startActivityForResult(AccountPicker.newChooseAccountIntent(null,
        null, new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null),
        REQ_ACCPICK);

activity yourself and deliver the resulting

email = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME)

to your setAccountName(email) as in:

   GAC = new GoogleApiClient.Builder(act)
      .addApi(Drive.API)
      .addScope(Drive.SCOPE_FILE)
      .addScope(Drive.SCOPE_APPFOLDER)
      .addConnectionCallbacks(...)
      .addOnConnectionFailedListener(...)
      ....
      .setAccountName(email)
      ....
      .build();

Good Luck


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...