我怎么能强制执行GoogleApiClient每次我打电话连接时提示帐户选择UI? [英] How can I enforce GoogleApiClient to prompt account chooser UI each time I call connect?

查看:789
本文介绍了我怎么能强制执行GoogleApiClient每次我打电话连接时提示帐户选择UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我运行code为极第一次在我的第一个应用程序推出的周期

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.

在这里输入的形象描述

不过,如果previous连接是成功的,我再为第一次在我的第二个应用程序推出的周期

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

帐户选择将不再弹出。 GoogleApiClient 将要使用的帐户名,我选择在previous应用程序启动循环。

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.

我碰到<少时href=\"http://stackoverflow.com/questions/31143973/how-to-clear-googleapiclient-default-account-and-credentials\">How要清除GoogleApiClient默认帐户和凭证

建议的解决方案不适合我的情况下工作。

The proposed solution doesn't work for my case.

mGoogleApiClient.clearDefaultAccountAndReconnect()

如果我已经连我的previous应用周期,我调用上面code第一次在我当前应用程序周期,我会得到下面的异常。

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)

以下code将不能工作。

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();
}

可我知道,我怎么能强制执行GoogleApiClient提示帐户选择UI每次我打电话的时间连接?

推荐答案

在这两个 GDAA REST API,您有两种选择:结果
1 /你不指定帐户和底层系统管理。结果
2 /你自己管理的帐户。

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.

如果您使用第一种方法,你永远不会知道谁选你的应用程序的用户。你只能干净的账户由 clearDefaultAccountAndReconnect 的手段。选择对话框再次弹出,用户可以选择(添加)另一个帐户。

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.

如果您需要了解当前选定的用户帐户(即缓存/持久性),你必须自己管理帐户的选择,你可以看到<一个href=\"https://github.com/seanpjanson/RESTDemo/blob/master/app/src/main/java/com/spjanson/restdemo/MainActivity.java\"相对=nofollow>这里(用于REST)或<一个href=\"https://github.com/seanpjanson/GDAADemo/blob/master/app/src/main/java/com/spjanson/gdaademo/MainActivity.java\"相对=nofollow>这里(对于GDAA) - 只需按照 REQ_ACCPICK 步道和 UT.AM 类。这样,你会在完全控制。

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)

你的 setAccountName(电子邮件)如:

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

好运

这篇关于我怎么能强制执行GoogleApiClient每次我打电话连接时提示帐户选择UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆