开发者

How to use built-in download manager on Android

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 t开发者_开发知识库he user downloads something, a download status is shown in the notification bar.

Sorry about my English!


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


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜