繁体   English   中英

如何检查下载是否正在运行? 下载管理器

[英]How to check if a download is running ? DownloadManager

我想检查当前是否正在下载。 我正在使用下面的代码:

public static boolean isDownloading(Context context){

    DownloadManager.Query query = null;
    Cursor c = null;
    DownloadManager downloadManager = null;
    downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
    query = new DownloadManager.Query();
    if(query!=null) {

        //return true;
        query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PAUSED|DownloadManager.STATUS_SUCCESSFUL|
                DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_PENDING);
    } else {
        Log.i("AUTOMATION_DOW" , "NO ");
        return false;
    }
    c = downloadManager.query(query);
    if(c.moveToFirst()) {
        int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
        switch(status) {
            case DownloadManager.STATUS_PAUSED:
                Log.i("AUTOMATION_DOWNLOAD","PAUSED");
               break;
            case DownloadManager.STATUS_PENDING:
                Log.i("AUTOMATION_DOWNLOAD","PENDING");
                break;
            case DownloadManager.STATUS_RUNNING:
                Log.i("AUTOMATION_DOWNLOAD","RUNNING");
                break;
            case DownloadManager.STATUS_SUCCESSFUL:
                Log.i("AUTOMATION_DOWNLOAD","SUCCESSFUL");
                break;
            case DownloadManager.STATUS_FAILED:
                Log.i("AUTOMATION_DOWNLOAD","FAILED");
                break;
        }
    }
    Log.i("AUTOMATION_DOWNLOAD","DEFAULT");
    c.close();
    return true;
}

即使正在运行下载,我也有以下logcat: 09-04 09:39:42.381 30213-31215/com.bytel.velizy.automation I/AUTOMATION_DOWNLOAD: DEFAULT我试图将以前的代码简化为该代码,但没有工作。

DownloadManager.Query query = null;
Cursor c = null;
DownloadManager downloadManager = null;
downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
query = new DownloadManager.Query();
if(query!=null) {
    return true;
     } else {
    return false;
}

如果要获取特定下载的状态,在那种情况下,当您在那时候开始下载时,downloadManager.enqueue()方法将返回每个下载唯一的downloadId,因此您可以保存该文件并使用它来获取DownloadStatus

从这里开始下载返回你的downloadId

 public long startDownload(String downloadurl) {
                DownloadManager downloadManager =
                        (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                try {
                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadurl));
// enqueue method retuns downloadId
                    return  downloadManager.enqueue(request);
                } catch (Exception e) {
                    Log.d("DOWNLOADED_INFO", "startDownload =" + e.getMessage());
                    e.printStackTrace();
                }
                return 0;
            }

此downloadId传递给getStatus方法

public static int getStatus(Context context , long downloadId) {
    DownloadManager downloadManager =
            (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(downloadId);// filter your download bu download Id
    Cursor c = downloadManager.query(query);
    if (c.moveToFirst()) {
        int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
        c.close();
        Log.i("DOWNLOAD_STATUS", String.valueOf(status));
        return status;
    }
    Log.i("AUTOMATION_DOWNLOAD", "DEFAULT");
    return -1;
}   

检查当前文件是否正在下载

public static boolean isDownloading(Context context , long downloadId){
        return getStatus(context , downloadId) == com.mozillaonline.providers.DownloadManager.STATUS_RUNNING;
    }

上面的方法用于检查特定下载的状态。 如果要检查下载文件的状态或下载文件处于暂停状态,可以通过此方法检查

public static boolean checkStatus(Context context , int status) {
        DownloadManager downloadManager = (DownloadManager)
                context.getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Query query = new DownloadManager.Query();

        query.setFilterByStatus(status);
        Cursor c = downloadManager.query(query);
        if (c.moveToFirst()) {
            c.close();
            Log.i("DOWNLOAD_STATUS", String.valueOf(status));
            return true;
        }
        Log.i("AUTOMATION_DOWNLOAD", "DEFAULT");
        return false;
    }

然后只需调用此方法

checkStatus(context , DownloadManager.STATUS_RUNNING);

您可以使用STATUS_RUNNING访问下载管理器的状态

https://developer.android.com/reference/android/app/DownloadManager.html#STATUS_RUNNING

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM