Android系统下载DownloadManager

本文地址:http://blog.csdn.net/whyrjj3/article/details/8000740

android2.3及以后,系统把内部的下载程序开放出来了。让我们可以使用DownloadManager这个类了。使用方法如下:


[java]  view plain copy
  1.     DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);  
  2.           
  3.     Uri uri = Uri.parse("fileUrl");  
  4.     Request request = new Request(uri);  
  5.   
  6.     //设置允许使用的网络类型,这里是移动网络和wifi都可以    
  7.     request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE|DownloadManager.Request.NETWORK_WIFI);    
  8.   
  9.     //禁止发出通知,既后台下载,如果要使用这一句必须声明一个权限:android.permission.DOWNLOAD_WITHOUT_NOTIFICATION    
  10.     //request.setShowRunningNotification(false);    
  11.   
  12.     //不显示下载界面    
  13.     request.setVisibleInDownloadsUi(false);  
  14.         /*设置下载后文件存放的位置,如果sdcard不可用,那么设置这个将报错,因此最好不设置如果sdcard可用,下载后的文件        在/mnt/sdcard/Android/data/packageName/files目录下面,如果sdcard不可用,设置了下面这个将报错,不设置,下载后的文件在/cache这个  目录下面*/  
  15. //request.setDestinationInExternalFilesDir(this, null, "tar.apk");  
  16. long id = downloadManager.enqueue(request);  
  17. //TODO 把id保存好,在接收者里面要用,最好保存在Preferences里面  

这里注意的是这时候程序就必须至少声明两个权限:

[html]  view plain copy
  1. <uses-permission android:name="android.permission.INTERNET" />    
  2.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  


用这个方法有好处,用户可以控制下载的过程,即如果没有下载完,并且不想下载了,可以终止下载,并且可以注册一个广播接收者,如果文件一下载完,就可以接收到一个广播。然后可以得到下载后的文件的路径:


[java]  view plain copy
  1. package cn.dotcreate.testProcess;  
  2.   
  3. import android.app.DownloadManager;  
  4. import android.app.DownloadManager.Query;  
  5. import android.content.BroadcastReceiver;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.database.Cursor;  
  9. import android.net.Uri;  
  10. import android.widget.Toast;  
  11.   
  12. public class CompleteReceiver extends BroadcastReceiver {  
  13.   
  14.     private DownloadManager downloadManager;  
  15.   
  16.     @Override  
  17.     public void onReceive(Context context, Intent intent) {  
  18.           
  19.         String action = intent.getAction();  
  20.         if(action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {  
  21.             Toast.makeText(context, "下载完成了....", Toast.LENGTH_LONG).show();  
  22.               
  23.             long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);                                                                                      //TODO 判断这个id与之前的id是否相等,如果相等说明是之前的那个要下载的文件  
  24.             Query query = new Query();  
  25.             query.setFilterById(id);  
  26.             downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);  
  27.             Cursor cursor = downloadManager.query(query);  
  28.               
  29.             int columnCount = cursor.getColumnCount();  
  30.             String path = null;                                                                                                                                       //TODO 这里把所有的列都打印一下,有什么需求,就怎么处理,文件的本地路径就是path  
  31.             while(cursor.moveToNext()) {  
  32.                 for (int j = 0; j < columnCount; j++) {  
  33.                     String columnName = cursor.getColumnName(j);  
  34.                     String string = cursor.getString(j);  
  35.                     if(columnName.equals("local_uri")) {  
  36.                         path = string;  
  37.                     }  
  38.                     if(string != null) {  
  39.                         System.out.println(columnName+": "+ string);  
  40.                     }else {  
  41.                         System.out.println(columnName+": null");  
  42.                     }  
  43.                 }  
  44.             }  
  45.             cursor.close();  
  46.         //如果sdcard不可用时下载下来的文件,那么这里将是一个内容提供者的路径,这里打印出来,有什么需求就怎么样处理                                                   if(path.startsWith("content:")) {  
  47.                                cursor = context.getContentResolver().query(Uri.parse(path), nullnullnullnull);  
  48.                                columnCount = cursor.getColumnCount();  
  49.                                while(cursor.moveToNext()) {  
  50.                                     for (int j = 0; j < columnCount; j++) {  
  51.                                                 String columnName = cursor.getColumnName(j);  
  52.                                                 String string = cursor.getString(j);  
  53.                                                 if(string != null) {  
  54.                                                      System.out.println(columnName+": "+ string);  
  55.                         }else {  
  56.                             System.out.println(columnName+": null");  
  57.                         }  
  58.                     }  
  59.                 }  
  60.                 cursor.close();  
  61.             }  
  62.               
  63.         }else if(action.equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)) {  
  64.             Toast.makeText(context, "点击<span style="font-family: 宋体; ">通知</span><span style="font-size: 10.5pt; text-indent: 21pt; font-family: 宋体; ">了....", Toast.LENGTH_LONG).show();</span>  
  65.         }  
  66.     }  
  67. }  

在清单里面注册当前这个receiver:

[html]  view plain copy
  1. <receiver android:name=".CompleteReceiver">  
  2.             <intent-filter>  
  3.                 <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>  
  4.                 <action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"/>  
  5.             </intent-filter>  
  6.         </receiver>  

    需要说明的是在如果手机的sdcard可用,那么在上面那一步里面的path就是sdcard上面的路径,如果sdcard不可用,那么那个路径将是一个内容提供者的路径。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值