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

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

问题描述

这是我的问题. 我正在尝试通过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天全站免登陆