目前我正在尝试使用 DownloadManager 下载文件,但这不起作用,下载开始,但下载后下载文件夹内没有文件。
这就是我的代码:
private void downloadAddon() {
try{
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
// request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setTitle("download");
request.setDescription("apk downloading");
// request.setAllowedOverRoaming(false);
request.setDestinationUri(Uri.fromFile(new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) , "mod.mcpack")));
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
long downloadID = downloadManager.enqueue(request);
//Just for testing
if (downloadComplete(downloadID)) {
Toast.makeText(this, "Download Status: Completed", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Download Status: Error", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
//Not required, there is no error that crashes the app
Toast.makeText(this, "Error catched: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private boolean downloadComplete(long downloadId){
DownloadManager …Run Code Online (Sandbox Code Playgroud)