Java 如何检查下载是否正在运行?下载管理器
我想检查当前是否正在运行下载。我正在使用下面的代码:Java 如何检查下载是否正在运行?下载管理器,java,android,android-download-manager,Java,Android,Android Download Manager,我想检查当前是否正在运行下载。我正在使用下面的代码: public static boolean isDownloading(Context context){ DownloadManager.Query query = null; Cursor c = null; DownloadManager downloadManager = null; downloadManager = (DownloadManager)context.getSystemService
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_下载:默认
我曾试图将以前的代码简化为这段代码,但没有成功
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访问下载管理器的状态
如果您想在此时开始下载时获取特定下载的状态,则downloadManager.enqueue()方法返回每个下载的唯一下载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;
}
此下载ID已传递给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);
然后简单地调用这个方法
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);
Cursor c=downloadManager.query(new downloadManager.query().setFilterById(referenceId));if(c.moveToFirst(){int status=c.getInt(c.getColumnIndex(DownloadManager.COLUMN_status));if(status==DownloadManager.status_SUCCESSFUL){
我不知道如何获取下载的id,所以我使用了标志。您是如何捕获referenceId的?当您像这样在下载管理器中添加请求时referenceId=downloadManager.enqueue(请求)这是针对你自己添加下载请求的情况。我现在明白了……我打开了一个网页,开始下载。我将修改我的代码以获得ID。你不必打开网页来开始下载。你可以在程序中添加代码,如<代码>字符串Appuri=DubString;URI DeLoad thuri=URI.PAR。se(appURI);DownloadManager.Request-Request=new DownloadManager.Request(Download_Uri);Request.setDestinationUri(Uri.fromFile(myFile));downloadReference=DownloadManager.enqueue(Request);
非常感谢您在我的帖子上花费的时间。现在我的代码运行得很好。函数startDownload()和checkStatus()正是我想要的。