How to use built-in download manager on Android

后端 未结 3 1019
-上瘾入骨i
-上瘾入骨i 2021-02-06 10:26

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 A

相关标签:
3条回答
  • 2021-02-06 10:41

    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.

    0 讨论(0)
  • 2021-02-06 10:52

    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);
    
     }
    }
    
    0 讨论(0)
  • 2021-02-06 11:01

    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:

    • Refresh progress bar in notification bar
    • Progress bar in notification bar when uploading image?

    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...

    0 讨论(0)
提交回复
热议问题