android – 如何接收下载管理器意图的状态,直到下载成功或失败

前端之家收集整理的这篇文章主要介绍了android – 如何接收下载管理器意图的状态,直到下载成功或失败前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的问题.
我正试图通过Asynctask使用下载管理器意图从我的服务器下载文件.
在asinctask类的doInBackground中,我调用了下载管理器intent,doinBackground将在下载完成时返回布尔值(成功或失败).
这是我的代码
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.
对我有什么帮助吗?

解决方法

您必须重新查询下载管理器.即使数据发生变化,光标也保持不变.试试这样:
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;
    }    
}
原文链接:https://www.f2er.com/android/315429.html

猜你在找的Android相关文章