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

浏览:
字体:
发布时间:2013-12-09 23:23:33
来源:

摘自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			}         }        }

						        

>更多相关文章
24小时热门资讯
24小时回复排行
资讯 | QQ | 安全 | 编程 | 数据库 | 系统 | 网络 | 考试 | 站长 | 关于东联 | 安全雇佣 | 搞笑视频大全 | 微信学院 | 视频课程 |
关于我们 | 联系我们 | 广告服务 | 免责申明 | 作品发布 | 网站地图 | 官方微博 | 技术培训
Copyright © 2007 - 2024 Vm888.Com. All Rights Reserved
粤公网安备 44060402001498号 粤ICP备19097316号 请遵循相关法律法规
');})();