草庐IT

java - UserRecoverableAuthIOException 无意使用 Drive SDK for Android

coder 2023-12-15 原文

我已经在我的 android 应用程序中实现了 google drive,它工作得很好,但我正在尝试找出一种在后台线程中运行上传/下载的方法,这样我就可以离开一个 Activity 并做其他事情在我的应用程序上。问题是,驱动器在出现异常时需要 Activity 引用,例如 UserRecoverableAuthIOException

这是我无法理解的问题。这是一些 try/catch 代码:

try {
    //...drive api stuff here
} catch (UserRecoverableAuthIOException e) {
    possibleException = e;
    try {
        e.getIntent();
    } catch ( NullPointerException e2 ) {  //this is the crazy part
        // e.getIntent() should not throw a nullpointer
        e2.printStackTrace();
        possibleException = e2;
    }
    onActivityRestartWhat = RESTART_IMPORT;
    // just a note i do handle this exception properly in another section of a code when there is an intent.
} catch (FileNotFoundException e) {
    possibleException = e;
    e.printStackTrace();
} catch (IOException e) {
    possibleException = e;
    e.printStackTrace();
}

我不明白为什么 UserRecoverableAuthIOException 在我尝试访问 getIntent 时抛出 NullPointerException

更多信息

当需要更多身份验证时,我会捕获 UserRecoverableAuthIOException 并通过 startActivityForResult 方法请求它。此外,只有当我退出已启动的 Activity 时才会抛出此异常,也就是销毁该 Activity 。基本上,我有一个在线程中上传/下载驱动文件的过程,如果我在完成之前不离开 Activity ,它就会工作,如果我通过后退按钮销毁 Activity ,那么我会得到这个异常。

堆栈跟踪

07-10 14:45:32.903: W/System.err(1450): java.lang.NullPointerException
07-10 14:45:32.913: W/System.err(1450):     at android.content.Intent.<init>      (Intent.java:3529)
07-10 14:45:32.913: W/System.err(1450):     at    com.google.android.gms.auth.UserRecoverableAuthException.getIntent(Unknown Source)
07-10 14:45:32.913: W/System.err(1450):     at com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException.getIntent(UserRecoverableAuthIOException.java:62)
07-10 14:45:32.913: W/System.err(1450):     at my.app.DriveHelper$2.run(DriveHelper.java:211)

我的愿望

我想在后台线程中运行下载/上传(通过谷歌驱动器)。由于 sdk 需要 startActivityForResult 和其他可能需要引用 ActivityContext 的方法,这使得这很困难,但一旦应用程序已被授予需要这些引用的 sdk 权限。希望这是有道理的。

最佳答案

以下是正确处理 UserRecoverableAuthIOException 异常的步骤,您甚至可以避免在按下后退时出现该异常。

在某些情况下,如果您收到该错误,则意味着该 Activity 已被破坏,因此您不应依赖于该 Activity

  • 你需要 从创建新的 com.google.api.services.tasks.Tasks 对象 ServiceContext 不是来自任何 Activity 直接如“tasks-android-sample”中所示

当你得到异常时

  1. 您需要显示来自 Service

    的带有 PendingIntent 的通知
  2. PendingIntent 应该包含对 Activity 的引用,比如 首页 Activity

  3. Activity 应该额外处理 Intent 并且应该做 所需的东西,比如显示选择帐户对话框

您可以查看示例代码here (谷歌任务服务)

关于java - UserRecoverableAuthIOException 无意使用 Drive SDK for Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17428066/

有关java - UserRecoverableAuthIOException 无意使用 Drive SDK for Android的更多相关文章

随机推荐