博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android使用DownloadManager实现文件下载
阅读量:6869 次
发布时间:2019-06-26

本文共 2978 字,大约阅读时间需要 9 分钟。

Android使用DownloadManager实现文件下载

下载

创建下载链接

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

设置允许下载的网络环境

request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
  • WIFI网络 : DownloadManager.Request.NETWORK_WIFI
  • 移动网络 : DownloadManager.Request.NETWORK_MOBILE

Notification显示下载进度

// 在Notification显示下载进度request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);// 设置Titlerequest.setTitle("更新");// 设置描述request.setDescription("正在下载更新文件...");

设置保存路径

private static final String DIR = "AutoUpdate";private static final String APK = "MyHome.apk";private static final String PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + DIR + "/" + APK;request.setDestinationInExternalPublicDir(DIR, APK);

下载

下载会返回一个进程ID

DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);long id = downloadManager.enqueue(request);

取消下载

通过ID可以需要下载

downloadManager.remove(id);

下载完成的监听

下载完成,系统会发出广播,通过注册广播监听者可以监听到下载完成

广播的Action为DownloadManager.ACTION_DOWNLOAD_COMPLETE

/** * Broadcast intent action sent by the download manager when the user clicks on a running * download, either from a system notification or from the downloads UI. */@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)public final static String ACTION_NOTIFICATION_CLICKED =        "android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED";

Code

下载

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));// WIFI状态下下载request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);// 设置通知栏request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);request.setTitle("更新");request.setDescription("正在下载更新文件...");// 存放路径request.setDestinationInExternalPublicDir(DIR, APK);// 开始下载DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);long id = downloadManager.enqueue(request);

广播接收者

注册

实现

package com.example.kongqingwei.downloadmanagerdemo;import android.app.DownloadManager;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;/** * Created by kongqingwei on 2016/12/19. * 广播接收者 */public class AutoUpdateBroadcastReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) { if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) { Toast.makeText(context, "下载完成", Toast.LENGTH_SHORT).show(); boolean isInstalled = AutoUpdater.installApk(); Toast.makeText(context, isInstalled ? "安装成功" : "安装失败", Toast.LENGTH_SHORT).show(); } }}

转载地址:http://ancfl.baihongyu.com/

你可能感兴趣的文章
ASP.NET MVC5+EF6+EasyUI 后台管理系统(81)-数据筛选(万能查询)
查看>>
如果觉得配置文件没有错,但web-dev-server总是报错,可以在hosts文件里加一行127.0.0.1 localhost...
查看>>
【转】分享8年开发经验,浅谈个人发展经历,明确自己发展方向
查看>>
If you only do what you can do you'll never be more than you are now.
查看>>
详解:基于WEB API实现批量文件由一个服务器同步快速传输到其它多个服务器功能...
查看>>
JavaScript获取浏览器类型与版本
查看>>
STM32普通定时器(TIM2-7)的时钟源
查看>>
[Android]使用Dagger 2进行依赖注入 - Producers(翻译)
查看>>
时序列数据库选型
查看>>
lamp安装
查看>>
QDEZ集训笔记【更新中】
查看>>
手工配置rsyslog配置文件详解
查看>>
composer安装
查看>>
Linux下快速迁移海量文件的操作记录
查看>>
windows环境redis主从安装部署
查看>>
mongodb指南
查看>>
su: user tomcat does not exist
查看>>
java 签名类 Signature
查看>>
非常详细的/etc/passwd解释
查看>>
解决Xcode在debug时不在断点处停止的方法<转>
查看>>