如何在下载成功或失败之前接收下载管理器意图的状态 [英] How to receive status of download manager intent until download success or failed

查看:11
本文介绍了如何在下载成功或失败之前接收下载管理器意图的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题.我正在尝试通过 Asynctask 使用下载管理器意图从我的服务器下载文件.在我的 asynctask 类的 doInBackground 中,我调用了下载管理器意图,当下载完成(成功或失败)时,doinBackground 将返回布尔值.这是我的代码

Here's my problem. I'm trying to download file from my server using download manager intent via Asynctask. in my doInBackground of asynctask class, i was call download manager intent, and doinBackground will return boolean value when download finish (Success or Failed). Here's my code

  protected Boolean doInBackground(String... f_url) {
        boolean flag = true;
        boolean downloading =true;
        try{
            DownloadManager mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);            
                           Request mRqRequest = new Request(
                                   Uri.parse("http://"+model.getDownloadURL()));
                           long idDownLoad=mManager.enqueue(mRqRequest);
                           DownloadManager.Query query = null;
                           query = new DownloadManager.Query();
                           Cursor c = null;
                             if(query!=null) {
                                        query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PAUSED|DownloadManager.STATUS_SUCCESSFUL|
                                                DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_PENDING);                                         
                             } else {
                        return flag;
                            }
                             c = mManager.query(query);
                                if(c.moveToFirst()) { 
            int status =c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); 


                               while (downloading)
                               {    Log.i ("FLAG","Downloading");
                                   if (status==DownloadManager.STATUS_SUCCESSFUL)
                                   {    Log.i ("FLAG","done");
                                       downloading = false;
                                       flag=true;
                                       break;      
                                   }
                                   if (status==DownloadManager.STATUS_FAILED)
                                   {Log.i ("FLAG","Fail");
                                       downloading = false;
                                       flag=false;
                                      break;
                                   }
                       c.moveToFirst();
                               }
        }
                            return flag;
        }
        catch (Exception e)
        {
             flag = false;
                return flag;
        }    
    }

但 DownloadManager 状态永远不会跳转到 DownloadManager.STATUS_SUCCESSFUL 或 DownloadManager.STATUS_FAILED.

But DownloadManager status never jump on DownloadManager.STATUS_SUCCESSFUL or DownloadManager.STATUS_FAILED.

推荐答案

您必须重新查询下载管理器.即使数据发生变化,光标也保持不变.试试这个:

You have to requery the download manager. The cursor stays the same even if the data changes. Try like this:

protected Boolean doInBackground(String... f_url) {
    boolean flag = true;
    boolean downloading =true;
    try{
        DownloadManager mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);            
        Request mRqRequest = new Request(
        Uri.parse("http://"+model.getDownloadURL()));
        long idDownLoad=mManager.enqueue(mRqRequest);
        DownloadManager.Query query = null;
        query = new DownloadManager.Query();
        Cursor c = null;
        if(query!=null) {
            query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PAUSED|DownloadManager.STATUS_SUCCESSFUL|DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_PENDING);                                         
        } else {
            return flag;
        }

        while (downloading) {
            c = mManager.query(query);
            if(c.moveToFirst()) { 
                Log.i ("FLAG","Downloading");
                int status =c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); 

                if (status==DownloadManager.STATUS_SUCCESSFUL) {
                    Log.i ("FLAG","done");
                    downloading = false;
                    flag=true;
                    break;      
                }
                if (status==DownloadManager.STATUS_FAILED) {
                    Log.i ("FLAG","Fail");
                    downloading = false;
                    flag=false;
                    break;
                }
            }
        }

        return flag;
    }catch (Exception e) {
        flag = false;
        return flag;
    }    
}

这篇关于如何在下载成功或失败之前接收下载管理器意图的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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