Android:下载管理器(DownloadManager),实现程序更新!
来源:互联网 发布:win7安装windows live 编辑:程序博客网 时间:2024/05/21 22:34
摘自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
- }
- }
- }
- <pre code_snippet_id="101016" snippet_file_name="blog_20131206_7_3275689"></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- Android:下载管理器(DownloadManager),实现程序更新!
- Android使用DownLoadManager(下载管理器)更新版本
- 使用DownloadManager实现下载更新
- android中DownloadManager实现版本更新,监听下载进度实例
- android下载新版本并更新(DownLoadManager、HttpURLConnection)
- DownloadManager实现下载、安装更新app功能
- Android 使用系统下载管理器DownloadManager下载文件
- Android-不使用downloadmanager更新下载
- Android 经典笔记之五:DownloadManager下载管理器介绍
- Ubuntu更新管理器下载程序
- Android通知栏实现APP下载更新(DownloadManager和自定义Notification2种方式实现)
- Android通知栏实现APP下载更新(DownloadManager和自定义Notification2种方式实现)
- Android使用DownloadManager实现版本更新
- Android studio 使用原生自带DownloadManager实现app下载更新
- Android 中 DownLoadManager 实现文件下载
- Android 中 DownLoadManager 实现文件下载
- Android使用DownloadManager实现文件下载
- MVP+DownloadManager实现android下载功能
- ASP.NET 学习路线图
- python的命令行解析argparse
- Android中Dialog的使用
- C++中的控制函数
- BZOJ2875//wikioi1281
- Android:下载管理器(DownloadManager),实现程序更新!
- 怎么才能恢复U盘中丢失掉的数据
- [算法3]Kmeans算法
- 【机器学习算法-python实现】svm支持向量机(2)—简化版SMO算法
- Leetcode_trapping-rain-water(updated c++ and python version)
- IOS——各种动画效果
- android nfc(官方翻译)
- mysql数据库数据录入正常,显示中文为问号的原因
- 指针函数与函数指针的区别