我從SD卡上讀取文件,並顯示在Gridview上。如何使用DownloadManager在android中停止文件下載?
當我從Gridview
中選擇項目並獲得項目的position
。
我點擊下載按鈕它會下載項目。
如何在使用downloadManager
時停止項目下載?
的代碼下載按鈕是這樣的:
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) ;
如何停止該項目下載文件時下載?
是的,它是工作,但它需要等待一段時間取消(刪除) – mehmet