Android:下载管理器(DownloadManager),实现程序更新!

来源:互联网 发布:win7安装windows live 编辑:程序博客网 时间:2024/05/21 22:34

摘自android官方文档: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 reboots. Instances 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.


  1. 1:manager =(DownloadManager)ctx.getSystemService(ctx.DOWNLOAD_SERVICE); //初始化下载管理器  
  2.   
  3. 2:  DownloadManager.Request request = new DownloadManager.Request(Uri.parse("www.xxx.com");//创建请求  
  4.   
  5. 3// 设置允许使用的网络类型,这里是移动网络和wifi都可以  
  6.      request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE| DownloadManager.Request.NETWORK_WIFI);  
  7.               
  8. // 禁止发出通知,既后台下载  
  9.     request.setShowRunningNotification(true);  
  10.               
  11. // 不显示下载界面  
  12.     request.setVisibleInDownloadsUi(true);  
  13.   
  14. 4:  SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-ddhh-mm-ss");  
  15.   
  16.     String date = dateformat.format(new Date());  
  17.               
  18.     request.setDestinationInExternalFilesDir(ctx, null,date+".apk");// 设置下载后文件存放的位置--如果目标位置已经存在这个文件名,则不执行下载,所以用date 类型随机取名。  
  19.               
  20.     manager.enqueue(request);// 将下载请求放入队列  
  21.   
  22. 5:在主界面创建下载完成接收器:    receiver = new DownloadCompleteReceiver();//创建下载完毕接收器  
  23.                 updateUtils.download();//执行下载  
  24.   
  25. 6:不要忘了在这个方法里注册广播接收器,系统下载完成会发送广播通知     
  26.     protected void onResume() {     
  27.         registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));     
  28.         super.onResume();     
  29.     }     
  30.   
  31. 7:  /** 
  32.     * DownloadManager下载完后 ,DOWNLOAD_SERVICE 会发送广播提示下载完成 
  33.     */  
  34.     public class DownloadCompleteReceiver extends BroadcastReceiver {     
  35.           
  36.          public void onReceive(Context context, Intent intent) {  
  37.             if (intent.getAction().equals(  
  38.                     DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {  
  39.   
  40.                 Toast.makeText(ctx, "下载完成!", Toast.LENGTH_LONG).show();  
  41.   
  42.                 String fileName = "";  
  43.   
  44.                 /** 
  45.                  * The download manager is a system service that handles long-running HTTP downloads. 
  46.                  */  
  47.                 DownloadManager downloadManager = (DownloadManager) ctx  
  48.                         .getSystemService(ctx.DOWNLOAD_SERVICE);//从下载服务获取下载管理器  
  49.   
  50.                 DownloadManager.Query query = new DownloadManager.Query();  
  51.   
  52.                 query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);//设置过滤状态:成功  
  53.   
  54.                 Cursor c = downloadManager.query(query);// 查询以前下载过的‘成功文件’  
  55.                   
  56.                 if (c.moveToFirst()) {// 移动到最新下载的文件  
  57.                     fileName = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));  
  58.                 }  
  59.   
  60.                 System.out.println("======文件名称=====" + fileName);  
  61.   
  62.                 File f = new File(fileName.replace("file://"""));// 过滤路径  
  63.   
  64.                 updateUtils.installApk(f);// 开始安装apk  
  65.   
  66.             }  
  67.          }     
  68.      }  

  1. <pre code_snippet_id="101016" snippet_file_name="blog_20131206_7_3275689"></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
0 0