6

I want to develop an application for the android platform that can download some files from my server.

How can i use android's download manager like it is used in the Android market app? I mean, the one when the user downloads something, a download status is shown in the notification bar.

Sorry about my English!

Juri
  • 32,424
  • 20
  • 102
  • 136
Thanh Le
  • 1,370
  • 2
  • 19
  • 30

3 Answers3

6

The one you mean is probably build into the Android market app. So I guess there is no way to easily reuse it but rather you'd have to build a similar one by yourself. You may want to check out the following SO posts for that purpose:

Depending on what kind of files you use I found the easiest way to download files from a remote server is to fire an intent like

Intent downloadIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pathToFile));

If the "pathToFile" is an HTTP address to a pdf document for instance, the Browser will automatically start a download which is shown in the notification bar and once it is finished, the user can click on it and (if an according app is installed) the file will be opened.

If you need to further process the downloaded files from within your app, then it's probably better to handle the download by yourself, opening an HTTP connection etc...

Community
  • 1
  • 1
Juri
  • 32,424
  • 20
  • 102
  • 136
4

You can also use the system's DownloadManager.

DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

Posting a DownloadManager.Request to this service results in the download appearing in the notification area.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
dokkaebi
  • 9,004
  • 3
  • 42
  • 60
  • Have you experienced a `SQLiteException` while using `DownloadManager`? I'm getting that error on 1 device and don't know why it happens. – rminaj Aug 08 '19 at 06:29
2

Use this class for downloading any file using download manager

  public class DLManager {
 @SuppressLint("NewApi")
 public static void useDownloadManager(String url, String name, Context c) {
  DownloadManager dm = (DownloadManager) c
    .getSystemService(Context.DOWNLOAD_SERVICE);
  DownloadManager.Request dlrequest = new DownloadManager.Request(
    Uri.parse(url));
  dlrequest
    .setAllowedNetworkTypes(
      DownloadManager.Request.NETWORK_WIFI
        | DownloadManager.Request.NETWORK_MOBILE)
    .setTitle("Title")
    .setDescription("Downloading in Progress..")
    .setDestinationInExternalPublicDir("Directory_name", name + ".jpg")
    .allowScanningByMediaScanner();

  dm.enqueue(dlrequest);

 }
}
Gagan
  • 745
  • 10
  • 31
  • works for me but i want listener that indicate my file downloaded – Abhishek Singh Dec 06 '16 at 07:02
  • there is no listeners available for this....but you can use broadcast manager for call backs...as there are broadcast receivers associated with download complete. – Gagan Dec 06 '16 at 07:13