public void init_download(String title, String filename, String url) { File path = new File(Environment.DIRECTORY_DOWNLOADS); if (!path.exists()) { path.mkdirs(); } Uri downloadUri = Uri.parse(url); DownloadManager.Request request = new DownloadManager.Request(downloadUri); request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE) .setAllowedOverRoaming(false) .setTitle(title) .setDescription(getResources().getString(R.string.downloading)) .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); } else { request.setShowRunningNotification(true); } long id = downloadManager.enqueue(request); //Save the request id SharedPreferences.Editor PrefEdit = shared_pref.edit(); PrefEdit.putLong("DOWNLOAD_ID", id); PrefEdit.commit(); }
} else { request.setShowRunningNotification(true);
public void startDownload(String url, String fileName) { Uri resource = Uri.parse(url); DownloadManager.Request request = new DownloadManager.Request(resource); request.setAllowedNetworkTypes(Request.NETWORK_WIFI); request.setAllowedOverRoaming(false); //set the file type MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url)); request.setMimeType(mimeString); //show notification request.setShowRunningNotification(true); request.setVisibleInDownloadsUi(true); //set target directory request.setDestinationInExternalPublicDir("YOURCACHEDIR", fileName); request.setTitle("Offline MAP downloading"); DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE); ; long id = downloadManager.enqueue(request); Log.i(TAG, "Start download, id = " + id); }