Reputation: 574
There is a feature in ADM (Download Manager) that if the user touches a download link (not a web page), the ADM(Download Manager) will be appeared as an application that has the ability to download files.
What should I do that if the user touched a download link, my application will be appeared a an application that has the ability to download files?
Upvotes: 1
Views: 87
Reputation: 654
DownloadData class
private long DownloadData (Uri uri, View v) {
long downloadReference;
// Create request for android download manager
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
//Setting title of request
request.setTitle("Data Download");
//Setting description of request
request.setDescription("Android Data download using DownloadManager.");
//Set the local destination for the downloaded file to a path
//within the application's external files directory
if(v.getId() == R.id.DownloadMusic)
request.setDestinationInExternalFilesDir(MainActivity.this,
Environment.DIRECTORY_DOWNLOADS,"AndroidTutorialPoint.mp3");
else if(v.getId() == R.id.DownloadImage)
request.setDestinationInExternalFilesDir(MainActivity.this,
Environment.DIRECTORY_DOWNLOADS,"AndroidTutorialPoint.jpg");
//Enqueue download and save into referenceId
downloadReference = downloadManager.enqueue(request);
Button DownloadStatus = (Button) findViewById(R.id.DownloadStatus);
DownloadStatus.setEnabled(true);
Button CancelDownload = (Button) findViewById(R.id.CancelDownload);
CancelDownload.setEnabled(true);
return downloadReference;
}
Description of the above code:
downloadReference: It is a unique id that we will refer for specific download request.
request: Instance of DownloadManager will be created through getSystemService by passing
DOWNLOAD_SERVICE. A new request is generated in the next statement using DownloadManager.Request(uri).
setDestinationInExternalFilesDir: This will be used to save file in external downloads folder.
downloadManager.enqueue(request): Enqueue a new download corresponding to request. The download will start automatically once the download manager is ready to execute it and connectivity is available.
Source :https://www.codeproject.com/Articles/1112730/Android-Download-Manager-Tutorial-How-to-Download
Upvotes: 1