5

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

这是我无法理解的问题。这是一些尝试/捕获代码:

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要扔NullPointerException我试图访问的乳清getIntent

更多信息

UserRecoverableAuthIOException当需要更多身份验证并通过该startActivityForResult方法请求时,我确实会抓住它。此外,仅当我退出已启动的活动(即销毁活动)时才会引发此异常。基本上,我有一个在线程中上传/下载驱动器文件的过程,如果我在完成之前不离开活动,它会起作用,如果我通过后退按钮破坏活动,那么我会得到这个异常。

堆栈跟踪

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和其他可能需要引用Activity或的方法Context使得这很困难,但是一旦应用程序被授予需要这些引用的 sdk 权限,它应该可以工作。希望这是有道理的。

4

2 回答 2

3

Below are the steps you can follow to handle UserRecoverableAuthIOException Exception properly and you can even avoid getting that exception when back pressed.

In some cases if you are receiving that error mean the activity is destroyed so you shouldn't depend on the activity

  • You need to create fresh com.google.api.services.tasks.Tasks object from Context of Service not from any Activity directly like shown in 'tasks-android-sample'

When you get Exception

  1. You need show a notification with PendingIntent from Service

  2. PendingIntent should contain the reference to an Activity , say HomeActivity

  3. Activity should handle the intent extra and should do the required things like showing choose account dialog

您可以在此处查看示例代码(GoogleTasksService)

于 2013-08-08T14:59:05.267 回答
0

What you can do, in this case don't use multiple activities.

By switching to views you can achieve your task.

于 2013-07-11T13:50:11.440 回答