如何检查下载是否正在运行? DownloadManager
我想检查下载是否正在运行。我使用的代码波纹管:如何检查下载是否正在运行? 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;
}
检查,如果当前文件downloadng
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);
我很感谢您在我的帖子上花费的时间。现在我的代码工作得很好。函数startDownload()和checkStatus()正是我所需要的。 – ilan
可以使用STATUS_RUNNING要访问下载管理器
https://developer.android.com/reference/android/app/DownloadManager.html#STATUS_RUNNING
'光标C = downloadManager.query(new DownloadManager.Query()。setFilterById(referenceId)); (c.moveToFirst()){int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));如果(c.moveToFirst if(status == DownloadManager.STATUS_SUCCESSFUL){' –
我不知道如何获得下载的id,所以我已经使用了标志。 请问您是如何看到referenceId的? – ilan
当您在下载管理器中添加请求时,像这样'referenceId = downloadManager.enqueue(request);'这是为了您自己添加下载请求的情况。 –