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

时间:2017-09-04 12:37:27

标签: java android android-download-manager

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

09-04 09:39:42.381 30213-31215/com.bytel.velizy.automation I/AUTOMATION_DOWNLOAD: DEFAULT

即使下载正在运行,我也有以下logcat: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; } 我试图将之前的代码减少到此但它没有用。

dfMain <- data.frame(last    = c("2017-08-01", "2017-08-01", "2017-08-05","2017-09-02","2017-09-02"),
                 previous    = c(NA, NA, "2017-08-01", "2017-08-05", "2017-08-01"),
                 personid    = c(12341, 122345, 12341, 12341, 122345),
                 diff        = c(NA, NA, 4, 28, 32))

2 个答案:

答案 0 :(得分:5)

如果你想获得特定下载的状态在那种情况下当你开始下载时,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);

答案 1 :(得分:0)

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

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