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

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;
}

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

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

如果您想获取特定下载的状态 在这种情况下,当您开始下载时 downloadManager.enqueue() 方法 returns downloadId 对于每个下载都是唯一的,因此您可以保存它并像这样获取 DownloadStatus

这里开始下载return你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);