我想检查当前是否正在运行下载。我正在使用下面的代码:
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;
}
2条答案
按热度按时间eblbsuwk1#
您可以使用status\u running来访问下载管理器的状态
https://developer.android.com/reference/android/app/downloadmanager.html#status_running
uz75evzq2#
如果您想在此时开始下载时获取特定下载的状态,downloadmanager.enqueue()方法返回downloadid,该ID对于每次下载都是唯一的,这样您就可以保存它并使用它来获取如下的下载状态
这里开始下载返回您的下载ID
此downloadid已传递给getstatus方法
检查当前文件是否正在下载
以上方法用于检查特定下载的状态。如果要检查下载管理器的状态,即任何文件正在下载或处于暂停状态,可以使用此方法进行检查
然后简单地调用这个方法