Android DownloadManager 的使用及断点续传

官方API:http://developer.android.com/reference/android/app/DownloadManager.html

Class Overview


The download manager is a system service that handles long-running HTTP downloads. Clients may request that a URI be downloaded to a particular destination file. The download manager will conduct the download in the background, taking care of HTTP interactions and retrying downloads after failures or across connectivity changes and system rebootsInstances of this class should be obtained through getSystemService(String) by passing DOWNLOAD_SERVICE. Apps that request downloads through this API should register a broadcast receiver for ACTION_NOTIFICATION_CLICKED to appropriately handle when the user clicks on a running download in a notification or from the downloads UI. Note that the application must have the INTERNET permission to use this class.

从Android 2.3(API level 9)开始Android用系统服务(Service)的方式提供了Download Manager来优化处理长时间的下载操作。Download Manager处理HTTP连接并监控连接中的状态变化以及系统重启来确保每一个下载任务顺利完成。

在大多数涉及到下载的情况中使用Download Manager都是不错的选择,特别是当用户切换不同的应用以后下载需要在后台继续进行,以及当下载任务顺利完成非常重要的情况(DownloadManager对于断点续传功能支持很好)。

要想使用Download Manager,使用getSystemService方法请求系统的DOWNLOAD_SERVICE服务,代码片段如下:

[java]  view plain copy
  1. String serviceString = Context.DOWNLOAD_SERVICE; 
  2. DownloadManager downloadManager; 
  3. downloadManager = (DownloadManager) getSystemService(serviceString); 
[java]  view plain copy
  1. String serviceString = Context.DOWNLOAD_SERVICE;  
  2. DownloadManager downloadManager;  
  3. downloadManager = (DownloadManager) getSystemService(serviceString);  

下载文件

要请求一个下载操作,需要创建一个DownloadManager.Request对象,将要请求下载的文件的Uri传递给Download Manager的enqueue方法,代码片段如下所示:

[java]  view plain copy
  1. String serviceString = Context.DOWNLOAD_SERVICE; 
  2. DownloadManager downloadManager; 
  3. downloadManager = (DownloadManager)getSystemService(serviceString); 
  4.  
  5. Uri uri = Uri.parse("http://developer.android.com/shareables/icon_templates-v4.0.zip"); 
  6. DownloadManager.Request request = new Request(uri); 
  7. long reference = downloadManager.enqueue(request); 
[java]  view plain copy
  1. String serviceString = Context.DOWNLOAD_SERVICE;  
  2. DownloadManager downloadManager;  
  3. downloadManager = (DownloadManager)getSystemService(serviceString);  
  4.   
  5. Uri uri = Uri.parse("http://developer.android.com/shareables/icon_templates-v4.0.zip");  
  6. DownloadManager.Request request = new Request(uri);  
  7. long reference = downloadManager.enqueue(request);  

在这里返回的reference变量是系统为当前的下载请求分配的一个唯一的ID,我们可以通过这个ID重新获得这个下载任务,进行一些自己想要进行的操作或者查询

下载的状态以及取消下载等等。

我们可以通过addRequestHeader方法为DownloadManager.Request对象request添加HTTP头,也可以通过setMimeType方法重写从服务器返回的mime type。

我们还可以指定在什么连接状态下执行下载操作。setAllowedNetworkTypes方法可以用来限定在WiFi还是手机网络下进行下载,setAllowedOverRoaming方法

可以用来阻止手机在漫游状态下下载。

下面的代码片段用于指定一个较大的文件只能在WiFi下进行下载:

[java]  view plain copy
  1. request.setAllowedNetworkTypes(Request.NETWORK_WIFI); 
[java]  view plain copy
  1. request.setAllowedNetworkTypes(Request.NETWORK_WIFI);  

Android API level 11 介绍了getRecommendedMaxBytesOverMobile类方法(静态方法),返回一个当前手机网络连接下的最大建议字节数,可以来判断下载

是否应该限定在WiFi条件下。

调用enqueue方法之后,只要数据连接可用并且Download Manager可用,下载就会开始。

要在下载完成的时候获得一个系统通知(notification),注册一个广播接受者来接收ACTION_DOWNLOAD_COMPLETE广播,这个广播会包含一个

EXTRA_DOWNLOAD_ID信息在intent中包含了已经完成的这个下载的ID,代码片段如下所示:

[java]  view plain copy
  1. IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE); 
  2.      
  3. BroadcastReceiver receiver = new BroadcastReceiver() { 
  4.   @Override 
  5.   public void onReceive(Context context, Intent intent) { 
  6.     long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); 
  7.     if (myDownloadReference == reference) { 
  8.       
  9.     } 
  10.   } 
  11. }; 
  12. registerReceiver(receiver, filter); 
[java]  view plain copy
  1. IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);  
  2.       
  3. BroadcastReceiver receiver = new BroadcastReceiver() {  
  4.   @Override  
  5.   public vo
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值