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

d5vmydt9  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(554)

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

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

即使正在运行下载,我也有以下日志: 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;
}
eblbsuwk

eblbsuwk1#

您可以使用status\u running来访问下载管理器的状态
https://developer.android.com/reference/android/app/downloadmanager.html#status_running

uz75evzq

uz75evzq2#

如果您想在此时开始下载时获取特定下载的状态,downloadmanager.enqueue()方法返回downloadid,该ID对于每次下载都是唯一的,这样您就可以保存它并使用它来获取如下的下载状态
这里开始下载返回您的下载ID

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

相关问题