Downloading in android devices i.e receiving data from remote system. In android devices Download manager is a system service that is used to download and manage any type and size of file. We have two nested classes first is DownloadManager.Query and second one is DownloadManager.Request. Query class is for filter download manager queries and request class is for necessary information to request a new download. Here is a little example to download a file.
public void downloadurl(String url, String article_heading) {
File direct = new File(Environment.getExternalStorageDirectory() + "/" + getString(R.string.app_name));
if (!direct.exists()) {
direct.mkdirs();
}
DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setDescription("Downloading pdf " + article_heading)
.setDestinationInExternalPublicDir("/" + getString(R.string.app_name), article_heading + ".pdf");
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
mgr.enqueue(request);
}
When download completes download manager send an action to the Broadcast intent and onReceive method is called.
BroadcastReceiver onComplete=new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
// Do Something
Toast.makeText(PdfViewerActivity.this, "Article successfully downloaded", Toast.LENGTH_LONG).show();
}
};
0 Comment(s)