android - DownloadManager - 收到 ACTION_DOWNLOAD_COMPLETE,但状态为 STATUS_RUNNING

标签 android android-download-manager

我在 ACTION_DOWNLOAD_COMPLETE 的 list 中注册了一个 BroadcastReceiver播送。

当我收到这个广播时,我提取下载id:

public class DownloadCompleteBroadcastReceiver
    extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent != null) {

            String action = intent.getAction();

            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                handleDownloadCompleteReceiver(context, intent);
            }
        }
    }

    private void handleDownloadCompleteReceiver(Context context, Intent intent) {

        long enqueueId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);

        if (enqueueId != -1) {

            Intent startServiceIntent = new Intent(context, HandleAPKDownloadCompleteIntentService.class);

            startServiceIntent.putExtra(HandleAPKDownloadCompleteIntentService.EXTRA_ENQUEUE_ID, enqueueId);

            context.startService(startServiceIntent);
        }
    }
}

我正在获取 enqueueId 的有效值并启动 IntentService 来处理已下载的文件:

@Override
protected void onHandleIntent(Intent intent) {

    long enqueueId = intent.getLongExtra(EXTRA_ENQUEUE_ID, -1);

    if (enqueueId == -1) {

        return;
    }

    DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

    DownloadManager.Query query = new DownloadManager.Query();

    query.setFilterById(enqueueId);

    Cursor c = dm.query(query);

    if (c != null) {

        if (c.moveToFirst()) {

            int statusColumnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
            int downloadManagerDownloadStatus = c.getInt(statusColumnIndex);

            if (DownloadManager.STATUS_SUCCESSFUL == downloadManagerDownloadStatus) {
                ...
                ...
            }
            else if (DownloadManager.STATUS_FAILED == downloadManagerDownloadStatus) {
                ...
                ...
            }
            else {
                reportToGoogleAnalyticsUnexpectedStatus(downloadManagerDownloadStatus);
            }

        }

        c.close();
    }
}

此时downloadManagerDownloadStatus = 2,根据documentationSTATUS_RUNNING

它没有任何意义,因为广播 ACTION_DOWNLOAD_COMPLETE 已经被调用,所以下载不应该运行。

我在谷歌分析中看到这种情况发生了很多次,但无法重现。

  • 知道为什么会这样吗?

  • 知道如何复制吗?

  • 我应该将此状态视为下载成功还是失败?

我真的不明白这样的下载是否成功,因为一方面 - 下载完成广播被触发,但另一方面状态正在运行。

值得一提的地方:我经常使用下载管理器:一次开始 10-15 次下载以触发应用中的特定流程,

提前致谢。

最佳答案

您提到您正在开始多个下载。

point that worth mentioning: I'm using download manager intensively: starts 10-15 downloads at once in trigger to particular flow in the app,

现在解释清楚 onHandleIntent 中发生了什么。

取自android docs

protected abstract void onHandleIntent (Intent intent)

This method is invoked on the worker thread with a request to process. Only one Intent is processed at a time, but the processing happens on a worker thread that runs independently from other application logic. So, if this code takes a long time, it will hold up other requests to the same IntentService, but it will not hold up anything else. When all requests have been handled, the IntentService stops itself, so you should not call stopSelf().

很可能您在一次或多次成功下载后触发了 DownloadManager.ACTION_DOWNLOAD_COMPLETE,然后线程处于 STATUS_RUNNING 状态,同时有多个下载未完成的原因。

如其他答案所述,您可能内存不足。我建议在每个阶段记录您的应用程序,并准确查看正在下载的内容以及卡住的位置。然后调查它停止的原因。

关于android - DownloadManager - 收到 ACTION_DOWNLOAD_COMPLETE,但状态为 STATUS_RUNNING,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33009602/

相关文章:

java - Android - 从BroadcastReceiver获取标题和描述

android - 下载的文件在 Android 4.4.2 中丢失

android - sendUserActionEvent() 为空

android - 如何设置每个 cardview 在 recyclerview 中的位置?

android - 将数据从自定义 View 传递到 Activity ,反之亦然

android - 找不到 MPAndroidChart Android 方法 setDrawVerticalGrid/setGridColor

java - Android Studio - 获取数字而不是 HTML 内容

Android 下载管理器路径有时似乎无效

android - Exoplayer如何显示下载进度

java - 如何使用 java 调用调用 native C++ 库的 C# 库