Android:下載管理器(DownloadManager),實現程序更新!

摘自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>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章