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: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

			}
         }   
     }




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值