如何使用DownloadManager在Android中停止文件下载? [英] How to stop the file download in android by using DownloadManager?
问题描述
当我从 Gridview $ c中选择项目时,我从SD卡中读取文件,并显示在Gridview上。 $ c>并获得项目的
位置
。
我点击下载按钮将下载该项目。 / p>
当我使用 downloadManager
时,如何停止项目下载?
代码是下载按钮,如下所示:
FileNode file = mFileList.get(temp_position); //获取我从Gridview
中选择的项目final String filename = file.mName.substring(file.mName.lastIndexOf(/)+ 1);
final String urlString =http://+ mIp + file.mName;
String serviceString = Context.DOWNLOAD_SERVICE;
DownloadManager downloadManager;
downloadManager =(DownloadManager)getActivity()。getSystemService(
serviceString);
Uri uri = Uri.parse(urlString);
DownloadManager.Request request = new Request(uri);
request.setTitle(filename);
request.setDescription(urlString);
String ext = filename.substring(filename.lastIndexOf(。)+ 1)
.toLowerCase(Locale.US);
String mimeType = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(ext);
Log.i(MIME,ext +==&+ mimeType);
if(mimeType!= null){
request.setMimeType(mimeType);
}
request.allowScanningByMediaScanner();
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(MainActivity.sAppName,filename);
downloadManager.enqueue(request);
文件下载时如何停止项目下载?
DownloadManager#enqueue
返回代表<$ c的 long
$ c> id 下载发生。将 long
保存在变量中。
然后,如果您需要取消下载,请致电 DownloadManager#remove()
传递那么久。
例如
//开始下载
long id = downloadManager.enqueue );
//停止下载
downloadManager.remove(id);
I read the file from the SD card , and show on the Gridview.
When I select the item from Gridview
and get the position
of item.
I click the download button it will download the item.
How to Stop the item download when I using the downloadManager
?
The code is download button is like the following:
FileNode file = mFileList.get(temp_position) ;//Get the item I have select from Gridview
final String filename = file.mName.substring(file.mName.lastIndexOf("/") + 1) ;
final String urlString = "http://" + mIp + file.mName ;
String serviceString = Context.DOWNLOAD_SERVICE ;
DownloadManager downloadManager ;
downloadManager = (DownloadManager) getActivity().getSystemService(
serviceString) ;
Uri uri = Uri.parse(urlString) ;
DownloadManager.Request request = new Request(uri) ;
request.setTitle(filename) ;
request.setDescription(urlString) ;
String ext = filename.substring(filename.lastIndexOf(".") + 1)
.toLowerCase(Locale.US) ;
String mimeType = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(ext) ;
Log.i("MIME", ext + " ==> " + mimeType) ;
if (mimeType != null) {
request.setMimeType(mimeType) ;
}
request.allowScanningByMediaScanner() ;
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) ;
request.setDestinationInExternalPublicDir(MainActivity.sAppName, filename) ;
downloadManager.enqueue(request) ;
How to Stop the item download when the file is downloading ?
DownloadManager#enqueue
returns a long
representing the id
of the download taking place. Save that long
in a variable.
Then, if you need to cancel the download, call DownloadManager#remove()
passing in that long.
Eg
//start a download
long id = downloadManager.enqueue(request);
//stop a download
downloadManager.remove(id);
这篇关于如何使用DownloadManager在Android中停止文件下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!