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);