安卓:如何使用下载管理器类?

28

我想从一个URL下载一个二进制文件。我是否可以使用我在这里找到的Android DownloadManager类(DownloadManager类)


是的,这是可能的。实际上,这就是创建这个类的原因。您可以查看此教程 https://androidclarified.com/android-downloadmanager-example/。 - Irshad Kumail
4个回答

48

我在这里找到了Android下载管理器类,是否可以使用?

是的,但只能在Android API Level 9(2.3版本)及以上版本中使用。这里有一个示例项目,演示了如何使用DownloadManager


1
@CommonsWare,我看了你的所有代码,都很棒。 我想要利用DownloadManager和wifi一起使用,也就是说,我需要使用SFTP从路由器下载文件,这可行吗? 能否请您为此指点迷津? - NovusMobile
2
@Satyam:DownloadManager 不支持 SFTP。据我所知,Android 中没有任何支持 SFTP 的东西。你需要找一个第三方的 JAR 包来实现。 - CommonsWare
我发现了一个在Java中使用的API名称为“JSCH”。 @CommonsWare 在我的Java项目中它运行得很完美,但在这里的Android中却不行。 这是可能的吗? 还是我遇到了其他问题? 请回复。 - NovusMobile
2
@Satyam:如果你在这个页面的右上角看到一个“提问”链接,那就是用来提问的。在StackOverflow上的评论可以用于澄清现有答案,但不能用于与此类无关的话题。 - CommonsWare
非常抱歉。 我已经在这里提出了问题:http://stackoverflow.com/questions/10927102/android-sftp-resumable-download-file 匆忙之中忘记将链接提交给您了...事实上,直到现在也没有人回复... - NovusMobile
非常有用的代码库! - Calin

23

使用DownloadManager类(仅适用于GingerBread及更高版本)

GingerBread引入了一个新功能——DownloadManager,它允许您轻松下载文件并将处理线程、流等繁重工作委托给系统。

首先,让我们看一个实用方法:

/**
 * @param context used to check the device version and DownloadManager information
 * @return true if the download manager is available
 */
public static boolean isDownloadManagerAvailable(Context context) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        return true;
    }
    return false;
}

方法的名称已经解释了一切。一旦确定DownloadManager可用,您可以执行以下操作:

String url = "url you want to download";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Some descrition");
request.setTitle("Some title");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.ext");

// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);

下载进度将会显示在通知栏中。


9
DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("http://www.example.com/myfile.mp3");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle("My File");
request.setDescription("Downloading");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationUri(Uri.parse("file://" + folderName + "/myfile.mp3"));
downloadmanager.enqueue(request);

9

确保在你的 Manifest.xml 文件中包含 READ_EXTERNAL_STORAGE 和 WRITE_EXTERNAL_STORAGE 权限:

接着在你的下载函数中加入以下代码:

if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
    || ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
    
    // this will request for permission when user has not granted permission for the app
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}

else{
    //Download Script
    DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    Uri uri = Uri.parse("URL of file to download");
    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setVisibleInDownloadsUi(true);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, uri.getLastPathSegment());
    downloadManager.enqueue(request);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接