Downloadmanager 状态整理

5 篇文章 0 订阅
1 篇文章 0 订阅
Download status:
1.STATUS_FAILED:(download has failed (and will not be retried)下载失败且不会再尝试下载
    1)ERROR_CANNOT_RESUME
some possibly transient error occurred but we can't resume the download
    2)ERROR_DEVICE_NOT_FOUND
no external storage device was found
    3)ERROR_FILE_ALREADY_EXISTS
the requested destination file already exists (the download manager will not overwrite an existing file)目标文件已存在,且不会覆盖文件
    4)ERROR_FILE_ERROR
a storage issue arises which doesn't fit under any other error code
    5)ERROR_HTTP_DATA_ERROR
an error receiving or processing data occurred at the HTTP level
    6)ERROR_INSUFFICIENT_SPACE
there was insufficient storage space
    7)ERROR_TOO_MANY_REDIRECTS
there were too many redirects
    8)ERROR_UNHANDLED_HTTP_CODE
an HTTP code was received that download manager can't handle
    9)ERROR_UNKNOWN
the download has completed with an error that doesn't fit under any other error code
2.STATUS_PAUSED:the download is waiting to retry or resume
    1)PAUSED_QUEUED_FOR_WIFI
the download exceeds a size limit for downloads over the mobile network and the download manager is waiting for a Wi-Fi connection to proceed
    2)PAUSED_UNKNOWN
the download is paused for some other reason
    3)PAUSED_WAITING_FOR_NETWORK
the download is waiting for network connectivity to proceed
    4)PAUSED_WAITING_TO_RETRY
the download is paused because some network error occurred and the download manager is waiting before retrying the request
3.STATUS_PENDING:the download is waiting to start
4.STATUS_RUNNING:the download is currently running
STATUS_SUCCESSFUL:the download has successfully completed



private static boolean isNeedDownload(long id, DownloadManager downloadManager){

        boolean isNeedDownloadAgain = true;

        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(id);
        Cursor cursor = downloadManager.query(query);
        if(cursor != null && cursor.moveToFirst()){
            int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
            int status = cursor.getInt(columnIndex);
            int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
            int reason = cursor.getInt(columnReason);

            switch(status){
                case DownloadManager.STATUS_FAILED:
                    switch(reason){
                        case DownloadManager.ERROR_CANNOT_RESUME:
                            //some possibly transient error occurred but we can't resume the download
                            break;
                        case DownloadManager.ERROR_DEVICE_NOT_FOUND:
                            //no external storage device was found. Typically, this is because the SD card is not mounted
                            break;
                        case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
                            //the requested destination file already exists (the download manager will not overwrite an existing file)
                            break;
                        case DownloadManager.ERROR_FILE_ERROR:
                            //a storage issue arises which doesn't fit under any other error code
                            break;
                        case DownloadManager.ERROR_HTTP_DATA_ERROR:
                            //an error receiving or processing data occurred at the HTTP level
                            break;
                        case DownloadManager.ERROR_INSUFFICIENT_SPACE://sd卡满了
                            //here was insufficient storage space. Typically, this is because the SD card is full
                            break;
                        case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
                            //there were too many redirects
                            break;
                        case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
                            //an HTTP code was received that download manager can't handle
                            break;
                        case DownloadManager.ERROR_UNKNOWN:
                            //he download has completed with an error that doesn't fit under any other error code
                            break;
                    }
                    isNeedDownloadAgain = true;

                    AlertUtil.alert("开始重新下载更新!", mContext);
                    break;
                case DownloadManager.STATUS_PAUSED:

                    switch(reason){
                        case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
                            //the download exceeds a size limit for downloads over the mobile network and the download manager is waiting for a Wi-Fi connection to proceed

                            break;
                        case DownloadManager.PAUSED_UNKNOWN:
                            //the download is paused for some other reason
                            break;
                        case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
                            //the download is waiting for network connectivity to proceed
                            break;
                        case DownloadManager.PAUSED_WAITING_TO_RETRY:
                            //the download is paused because some network error occurred and the download manager is waiting before retrying the request
                            break;
                    }
                    isNeedDownloadAgain = false;

                    AlertUtil.alert("下载已暂停,请继续下载!", mContext);
                    break;
                case DownloadManager.STATUS_PENDING:
                    //the download is waiting to start
                    isNeedDownloadAgain = false;
                    AlertUtil.alert("更新正在下载!", mContext);
                    break;
                case DownloadManager.STATUS_RUNNING:
                    //the download is currently running
                    isNeedDownloadAgain = false;
                    AlertUtil.alert("更新正在下载!", mContext);
                    break;
                case DownloadManager.STATUS_SUCCESSFUL:
                    //the download has successfully completed
                    isNeedDownloadAgain = false;
                    installApk(id, downloadManager, mContext);
                    break;
            }

        }
        return isNeedDownloadAgain;
    }


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DownloadManager.Query是Android系统中的一个类,用于查询下载任务的信息。通过使用DownloadManager.Query,可以获取下载任务的状态、文件大小、下载进度等信息。 DownloadManagerAndroid提供的一个系统服务,用于管理下载任务。它可以处理大文件的下载,并提供了断点续传的功能。而DownloadManager.Query则是用于查询下载任务的相关信息。 使用DownloadManager.Query需要以下步骤: 1. 获取DownloadManager实例:通过调用Context.getSystemService(Context.DOWNLOAD_SERVICE)方法获取DownloadManager的实例。 2. 创建DownloadManager.Query对象:通过new DownloadManager.Query()创建一个查询对象。 3. 设置查询条件:可以通过setQueryFilterByStatus()方法设置查询条件,例如只查询已完成的任务、正在进行中的任务等。 4. 执行查询:通过调用DownloadManager.query()方法执行查询,返回一个Cursor对象。 5. 解析查询结果:通过遍历Cursor对象,可以获取每个下载任务的相关信息,如下载ID、文件名、文件大小、下载状态等。 以下是一些常用的查询方法: - setQueryFilterByStatus(int status): 设置查询条件,根据下载状态过滤任务。常见的状态STATUS_PENDING(等待中)、STATUS_RUNNING(进行中)、STATUS_PAUSED(暂停中)、STATUS_SUCCESSFUL(已完成)等。 - setQueryFilterById(long... ids): 设置查询条件,根据下载ID过滤任务。 - getColumnIndex(String columnName): 获取指定列名在Cursor中的索引。 - getInt(int columnIndex): 获取指定列索引对应的整型值。 - getString(int columnIndex): 获取指定列索引对应的字符串值。 注意:使用DownloadManager.Query需要在AndroidManifest.xml文件中添加相应的权限,如android.permission.ACCESS_DOWNLOAD_MANAGER
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值