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 } } }
- 01-11全球最受赞誉公司揭晓:苹果连续九年第一
- 12-09罗伯特·莫里斯:让黑客真正变黑
- 12-09谁闯入了中国网络?揭秘美国绝密黑客小组TA
- 12-09警示:iOS6 惊现“闪退”BUG
- 03-08消息称微软开发内部AI推理模型,或将成为Op
- 03-08美国法院驳回马斯克请求,未阻止OpenAI转型
- 03-08饿了么成立即时配送算法专家委员会 持续全局
- 03-08长安汽车:预计今年底长安飞行汽车将完成试
- 03-08谷歌推出虚拟试穿、AR美妆新功能