Java Android:如何使用下载管理器类?
我想从url下载一个二进制文件。可以使用我在这里找到的Android下载管理器类吗 可以使用我在这里找到的android下载管理器类吗 是的,不过这只在Android API级别9(版本2.3)之后才可用。演示如何使用下载管理器类(仅姜饼和较新版本) 姜饼带来了一个新功能,DownloadManager,它允许您轻松下载文件,并将处理线程、流等的艰苦工作委托给系统 首先,让我们看一个实用方法:Java Android:如何使用下载管理器类?,java,android,url,download,Java,Android,Url,Download,我想从url下载一个二进制文件。可以使用我在这里找到的Android下载管理器类吗 可以使用我在这里找到的android下载管理器类吗 是的,不过这只在Android API级别9(版本2.3)之后才可用。演示如何使用下载管理器类(仅姜饼和较新版本) 姜饼带来了一个新功能,DownloadManager,它允许您轻松下载文件,并将处理线程、流等的艰苦工作委托给系统 首先,让我们看一个实用方法: /** * @param context used to check the device vers
/**
* @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);
下载进度将显示在通知栏中。确保您的Manifest.xml文件中包含读取外部存储和写入外部存储权限:
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);
然后在下载函数中包含此代码
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);
}
@Commonware..我浏览了你的所有代码。都很棒。我想通过wifi方式使用DownloadManager。我必须使用SFTP并从路由器下载文件。有可能吗?请为我提供指导。@Satyam:
DownloadManager
不支持SFTP。Android中没有任何东西支持SFTP AFAIK。你需要找到一个第三方JAR,我找到了我在java中使用的API名称“JSCH”。@在我的java项目中,Commonware运行得很完美,但在这里,Android却不是。。可能吗?或者我可以面对其他问题吗?请回复..@Satyam:如果你看这页的右上角,你会看到一个“提问”链接。用来提问的。StackOverflow中的评论可以用于澄清现有答案,但不能用于与此无关的主题。对此我深表歉意。我已经问了一个问题:在匆忙中忘记提交链接给你。。。这是真的,直到……没有人回答同样的……是的,这是可能的。事实上,这就是创建此类的原因。您可以查看本教程