如何使用下载管理器停止在Android上的文件下载? [英] How to stop the file download in android by using DownloadManager?

查看:457
本文介绍了如何使用下载管理器停止在Android上的文件下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读SD卡的文件,然后显示在GridView的。

I read the file from the SD card , and show on the Gridview.

当我从 GridView中的项目并获得项目的位置

我点击下载按钮,它会下载该项目。

I click the download button it will download the item.

如何停止使用我当项目下载下载管理器

How to Stop the item download when I using the downloadManager ?

在code是下载按钮是这样的:

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 ?

推荐答案

<一个href=\"http://developer.android.com/reference/android/app/DownloadManager.html#enqueue%28android.app.DownloadManager.Request%29\"><$c$c>DownloadManager#enqueue返回重新presenting下载发生的 ID 。在一个变量保存

DownloadManager#enqueue returns a long representing the id of the download taking place. Save that long in a variable.

然后,如果你需要取消下载,请拨打<一个href=\"http://developer.android.com/reference/android/app/DownloadManager.html#remove%28long...%29\"><$c$c>DownloadManager#remove()在经过那么长。

Then, if you need to cancel the download, call DownloadManager#remove() passing in that long.

例如:

//start a download
long id = downloadManager.enqueue(request);

//stop a download
downloadManager.remove(id);

这篇关于如何使用下载管理器停止在Android上的文件下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆