【问题标题】:Is there any way make account picker UI appear as fast as possible有什么方法可以让帐户选择器 UI 尽快出现
【发布时间】:2016-01-10 18:48:48
【问题描述】:

目前,我正在从 API Client Library for Java 迁移到 Drive API for Android

但是,与 Java 的 API 客户端库 相比,我意识到 Drive API for Android 的帐户选择器非常慢。 有时,等待时间长达 5 秒。

非常快(Java 的 API 客户端库)

private static final GoogleAccountCredential googleAccountCredential = GoogleAccountCredential.usingOAuth2(context, 
    Arrays.asList(
        DriveScopes.DRIVE_APPDATA,
        // Legacy. Shall be removed after a while...
        DriveScopes.DRIVE
    )
);

startActivityForResult(googleAccountCredential.newChooseAccountIntent(), RequestCode.REQUEST_ACCOUNT_PICKER_LOAD_FROM_CLOUD);

此帐户选择器几乎立即出现。


非常慢(适用于 Android 的 Drive API)

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

@Override
public void onConnectionFailed(ConnectionResult result) {
    Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
    if (!result.hasResolution()) {
        // show the localized error dialog.
        GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
        return;
    }
    try {
        result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
    } catch (SendIntentException e) {
        Log.e(TAG, "Exception while starting resolution activity", e);
    }
}

通常,当我第一次调用帐户选择器时,需要等待很长时间。有时,等待时间可能长达 5 秒。

我了解我们的应用程序代码通过 IPC 调用 Google Play 服务。因此,可能会有一些缓慢。不过,我预计它不会慢到 5 秒。

有什么办法可以让帐户选择器 UI 尽快出现吗?

【问题讨论】:

  • 已经讨论过here already。尝试将帐户选择器 (startActivityForResult(....)) 与“setAccountName(email)”一起使用。我没有遇到任何延误。
  • 谢谢。使用startActivityForResult 的帐户选择器我也不会遇到延迟。你有没有尝试过使用result.startResolutionForResult 来显示帐户选择器?您是否遇到任何延迟?因为我不确定延迟是否仅发生在我的设备中,或者这是一种常见情况。谢谢。

标签: android google-play-services google-drive-android-api


【解决方案1】:

如@seanpj 在https://stackoverflow.com/a/34706726/72437 中所述

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

会更快。

其中一个原因是,它不会在弹出对话框中显示您的个人资料图片。我认为这些头像需要网络活动,这会使处理过程变慢。

【讨论】:

    【解决方案2】:

    请尝试以下方法(我不使用addScope):

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER))
            .requestEmail()
            .build();
    
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .addApi(Drive.API)
            .build();
    

    帐户选择器出现得很快(我的手机:摩托罗拉,API 16)。

    【讨论】:

    • 谢谢。今晚打算试试。我想知道你能帮忙试验我的代码吗?我想知道这种缓慢是否仅发生在我自己的设备(Nexus 5)中,还是发生在大多数设备中?
    • 我试过你的代码,除了.enableAutoManage,没有任何显示。我试过的代码mGoogleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) .addApi(Drive.API) .addScope(Drive.SCOPE_APPFOLDER) //.addConnectionCallbacks(this) //.addOnConnectionFailedListener(this) .build(); mGoogleApiClient.connect();。它也显示得很快。
    • 这很奇怪。也许你需要等待更长的时间,比如 10 秒? (是的。有时需要很长时间)因为,代码只是从官方 Android Drive API 示例中复制粘贴 - github.com/googledrive/android-demos/blob/master/app/src/main/…
    • 抱歉。我忘记了,UI 是由result.startResolutionForResult 触发的。我修改了代码。
    • 我试过你修改过的代码,选择器显示也很快,IMO,你可以用其他手机或者甚至更快的网络(wifi、3G)进行测试? :)
    猜你喜欢
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多