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

Kri*_*ris 18 java url android download

我想从网址下载二进制文件.是否可以使用我在这里找到的Android下载管理器

Com*_*are 38

是否可以使用我在这里找到的android下载管理器类

是的,虽然这仅在Android API Level 9(版本2.3)之后可用.这是一个演示如何使用的示例项目DownloadManager.

  • @Satyam:`DownloadManager`不支持SFTP.Android中没有任何内容支持SFTP AFAIK.您需要为此找到第三方JAR. (2认同)
  • @Satyam:如果你查看本页面的右上角,你会看到一个"提问"链接.这用于提问.StackOverflow中的注释可用于澄清现有答案,但不适用于此类不相关的主题. (2认同)

小智 12

使用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;
}
Run Code Online (Sandbox Code Playgroud)

方法的名称解释了这一切.一旦确定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);
Run Code Online (Sandbox Code Playgroud)

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


小智 6

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);
Run Code Online (Sandbox Code Playgroud)


小智 6

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

然后将此代码包含在您的下载功能中

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);
}
Run Code Online (Sandbox Code Playgroud)