Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android:下載管理器(DownloadManager),實現程序更新!

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

編輯:關於Android編程


摘自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:manager =(DownloadManager)ctx.getSystemService(ctx.DOWNLOAD_SERVICE); //初始化下載管理器

2:	DownloadManager.Request request = new DownloadManager.Request(Uri.parse("www.xxx.com");//創建請求

3:// 設置允許使用的網絡類型,這裡是移動網絡和wifi都可以
  	 request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE| DownloadManager.Request.NETWORK_WIFI);
			
// 禁止發出通知,既後台下載
	request.setShowRunningNotification(true);
			
// 不顯示下載界面
	request.setVisibleInDownloadsUi(true);

4:	SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-ddhh-mm-ss");

	String date = dateformat.format(new Date());
			
	request.setDestinationInExternalFilesDir(ctx, null,date+".apk");// 設置下載後文件存放的位置--如果目標位置已經存在這個文件名,則不執行下載,所以用date	類型隨機取名。
			
	manager.enqueue(request);// 將下載請求放入隊列

5:在主界面創建下載完成接收器:	receiver = new DownloadCompleteReceiver();//創建下載完畢接收器
				updateUtils.download();//執行下載

6:不要忘了在這個方法裡注冊廣播接收器,系統下載完成會發送廣播通知	
	protected void onResume() {   
        registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));   
        super.onResume();   
    }   

7:	/**
	* DownloadManager下載完後 ,DOWNLOAD_SERVICE 會發送廣播提示下載完成
	*/
    public class DownloadCompleteReceiver extends BroadcastReceiver {   
     	
         public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(
					DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {

				Toast.makeText(ctx, "下載完成!", Toast.LENGTH_LONG).show();

				String fileName = "";

				/**
				 * The download manager is a system service that handles long-running HTTP downloads.
				 */
				DownloadManager downloadManager = (DownloadManager) ctx
						.getSystemService(ctx.DOWNLOAD_SERVICE);//從下載服務獲取下載管理器

				DownloadManager.Query query = new DownloadManager.Query();

				query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);//設置過濾狀態:成功

				Cursor c = downloadManager.query(query);// 查詢以前下載過的‘成功文件’
				
				if (c.moveToFirst()) {// 移動到最新下載的文件
					fileName = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
				}

				System.out.println("======文件名稱=====" + fileName);

				File f = new File(fileName.replace("file://", ""));// 過濾路徑

				updateUtils.installApk(f);// 開始安裝apk

			}
         }   
     }




						
		
	
				
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved