DownloadManager.ACTION_DOWNLOAD_COMPLETE广播接收机接收相同的下载ID具有不同的下载状态在Android中不止一次 [英] DownloadManager.ACTION_DOWNLOAD_COMPLETE broadcast receiver receiving same download id more than once with different download statuses in Android

查看:967
本文介绍了DownloadManager.ACTION_DOWNLOAD_COMPLETE广播接收机接收相同的下载ID具有不同的下载状态在Android中不止一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Android DownloadManger系统服务下载的方式如下一些文件

I am using Android DownloadManger System Service for downloading some files in following way

dwnId = mgr.enqueue(new DownloadManager.Request(serveruri)
        .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
                DownloadManager.Request.NETWORK_MOBILE)
                .setAllowedOverRoaming(false)
                .setTitle(getAlbumName())
                .setDescription(getTrackName())
                .setDestinationUri(deviceUri)
                .setShowRunningNotification(true));

其中,经理是下载管理器的实例, dwnId 是唯一的ID返回。我也注册了 ACTION_DOWNLOAD_COMPLETE

where mgr is Download Manager instance, dwnId is unique ID returned. I am also registering for ACTION_DOWNLOAD_COMPLETE

registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

和在onDownloadComplete的BroadcastReceiver的的onReceive()方法,我得到的卸载ID喜欢

and in the onDownloadComplete BroadcastReceiver's onReceive() method I am getting download Id like

Long dwnId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);

在这之后我查询下载管理器下载状态

After that I am querying Download Manager for Download status

Cursor c = downloadManager.query(new DownloadManager.Query().setFilterById(dwnId)); c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));

有关DownloadManager.STATUS_ *常量。

for DownloadManager.STATUS_* constants.

现在的问题是我收到两次相同的downId(指的onReceive方法被调用两次),一旦与DownloadManager.STATUS_SUCCESSFUL状态,一旦与同dwnId DownloadManager.STATUS_FAILED状态。我发出请求,下载一些10个文件的时间和但设备下载管理器,它显示的下载次数在通知栏左上角意味着一些12或13。我认为,下载管理器在下载文件中的某些问题,并恢复或自动重启再次下载同一个文件。这就是为什么是有区别的文件算我请下载并在下载队列实际数量。因为这只是我收到同样的DownloadId完整的动作两次。如果这是真的,如何限制它。我是什么可能是我要求实际下载的次数差异的原因错了吗?为什么广播接收器接收两次相同的下载标识。任何人都可以请让我知道?

The problem is I am receiving the same downId twice (means onReceive method is called twice), once with DownloadManager.STATUS_SUCCESSFUL status and once with DownloadManager.STATUS_FAILED status for same dwnId. I am issuing request to download some 10 files at a time and but on device download manager it is showing the download count as some 12 or 13 in the notification bar top left means. I think that Download manager has some problem in downloading files and resumed or automatically restarted to download the same file again. Thats why there is a difference between the files count I requested to download and actual number in download queue. Because of this only I am getting same DownloadId complete action twice. If this is true, how to restrict it. Am I wrong what might be the reason for count difference between what I requested to actual download? Why is the broadcast receiver receiving the same download Id twice. Can anybody please let me know?

在此先感谢...

推荐答案

这是报告的bug,请参阅:<一href="http://$c$c.google.com/p/android/issues/detail?id=18462">http://$c$c.google.com/p/android/issues/detail?id=18462

This is a reported bug see: http://code.google.com/p/android/issues/detail?id=18462

周围的办法,我发现是验证下载是否成功,如果没有沟的意图或重新排队的文件,如果它从来没有下载...

The way around I found is to verify if the download was a success, if not ditch the intent or re-queue the file if it was never downloaded...

失去一两个小时搞清楚一个:(

Lost a couple of hours figuring that one :(

**编辑:增加code例如**

** adding code example **

/**
 * Check if download was valid, see issue
 * http://code.google.com/p/android/issues/detail?id=18462
 * @param long1
 * @return
 */
private boolean validDownload(long downloadId) {

    Log.d(TAG,"Checking download status for id: " + downloadId);

    //Verify if download is a success
    Cursor c= dMgr.query(new DownloadManager.Query().setFilterById(downloadId));

    if(c.moveToFirst()){            
        int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));

        if(status == DownloadManager.STATUS_SUCCESSFUL){
            return true; //Download is valid, celebrate
        }else{
            int reason = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON));
            Log.d(TAG, "Download not correct, status [" + status + "] reason [" + reason + "]");            
            return false;
        }   
    }               
    return false;                                   
}

有关完整的code见:<一href="https://github.com/flegare/JAV387_LaboWidget/blob/master/src/com/mobidroid/widgetfact/service/FactService.java">https://github.com/flegare/JAV387_LaboWidget/blob/master/src/com/mobidroid/widgetfact/service/FactService.java

For complete code see : https://github.com/flegare/JAV387_LaboWidget/blob/master/src/com/mobidroid/widgetfact/service/FactService.java

这篇关于DownloadManager.ACTION_DOWNLOAD_COMPLETE广播接收机接收相同的下载ID具有不同的下载状态在Android中不止一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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