实现样式
- DownloadManager 是android官方下载通道,一般都是没有问题的,坑点就是不同厂商会做不同的优化,所以要注意Download 文件夹是否初始化(PS:外置卡可能没有Download文件夹)。
- 如果定制Rom不支持DownloadManager,最好的方式是跳转到浏览器进行下载,但是如何检测DownloadManager是否可用,我也是参考了别人的代码,如果有遇到问题的朋友,可以进行相关交流,参考链接放在文章尾部。
- 大体思路解析
-
下载完成之后,如果APP仍然在运行,则直接监听下载完成广播,直接启动APP
-
如果系统DownloadManager仍然正在下载,然而APP被杀死(APP启动后,下载线程先检测是否存在下载任务,有重复利用的任务时候,不新建任务:PS,android3.0之后,首次安装未启动,或者被强制杀死的APP,静态广播注册是接收不到系统广播的,所以只能在APP被启动的时候检测)
-
- 打开APP,启动更新线程的时候,检测是否存在
- DownloadManager 会保存各种下载状态任务(等待下载,下载中...,暂停,下载失败,下载成功),我们只要查询出这些状态下的任务,重复利用就进行了相当大的优化。
- 测试主要发现华为Mate9机型启动下载任务会延时,进行相关优化,发现还是存在相应的问题,于是采用浏览器下载进行兼容。(PS:估计国内其他机型也会遇到类似的问题,当然自己实现下载通道也是可以的)
- 查询手机DownloadManager 任务,确认下载任务是否已经存在
public CursorBean queryProcess(String fileName) {
CursorBean targetBean = null;
if (TextUtils.isEmpty(fileName)) {
return targetBean;
}
int status_query = DownloadManager.STATUS_PENDING | DownloadManager.STATUS_RUNNING
| DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_SUCCESSFUL | DownloadManager.STATUS_FAILED;
Cursor c = null;
try {
c = getCursor(true, status_query);
while (c.moveToNext()) {
CursorBean queryBean = new CursorBean();
long downId = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID));
int reason = c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON));
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
String uri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_URI));
String title = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
String local_url = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
String media_type = c.getString(c.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE));
String description = c.getString(c.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION));
long timeStamp = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));
Log.d("download-kk", "description | " + description + "\r\n"
+ "uri | " + uri + "\r\n"
+ "media_type |" + media_type + "\r\n"
+ "local_url |" + local_url + "\r\n");
String filePath = getDownloadFile(c);
queryBean.setId(downId);
queryBean.setTitle(title);
queryBean.setFile(filePath);
queryBean.setStatus(status);
queryBean.setTimeStamp(timeStamp);
if (!TextUtils.isEmpty(filePath) && fileName.contains(fileName)) {
if (reason != DownloadManager.PAUSED_WAITING_TO_RETRY
&& reason != DownloadManager.STATUS_FAILED
&& reason != DownloadManager.STATUS_PAUSED) {
if (null == targetBean) {
targetBean = new CursorBean();
}
if (timeStamp > targetBean.getTimeStamp()) {
if (targetBean != null && targetBean.getId() != 0) {
getDownloadMgr().remove(downId);
}
targetBean = queryBean;
}
} else {
try {
getDownloadMgr().remove(downId);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
* 启动新的下载任务(注意开始的初始化文件夹,这个是一个坑)
public long download(String title, String defName, String url) {
try {
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdirs();
final Uri parse = Uri.parse(url);
final DownloadManager.Request request = new DownloadManager.Request(parse);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
request.setAllowedOverRoaming(false);
request.setMimeType(MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url)));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(true);
request.setTitle(String.valueOf(title));
//set this to fileName
url = getFileName(defName, parse);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, url);
return ((DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request);
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
* 重复利用已经存在的任务
@Override
public void run() {
String fileName = mLoadMgr.getFileName(DEF_APP_NAME, mUrl);
CursorBean historyBean = mLoadMgr.queryProcess(fileName);
if (historyBean != null && historyBean.getId() != -1) {//证明有下载的线程
int status = historyBean.getStatus();
switch (status) {
case DownloadManager.STATUS_SUCCESSFUL:
long timeDiff = System.currentTimeMillis() - historyBean.getTimeStamp();
double hour = (timeDiff) / 1000D / 60D / 60D;//时效是一个小时
boolean exists = new File(historyBean.getFile()).exists();
if (hour <= 1 && timeDiff > 0 && exists) {
if (mHandler != null) {
Message msg = Message.obtain();
msg.what = PROCESS;
msg.obj = new Integer(100);
mHandler.sendMessage(msg);
}
mLoadMgr.install(historyBean.getFile(), AUTHORITY_PATH);
} else {
mLoadId = mLoadMgr.reload(mLoadId, mLoadName, DEF_APP_NAME, mUrl);
}
break;
case DownloadManager.STATUS_PENDING:
case DownloadManager.STATUS_RUNNING:
case DownloadManager.STATUS_PAUSED:
mLoadId = historyBean.getId();
break;
default:
mLoadId = mLoadMgr.reload(mLoadId, mLoadName, DEF_APP_NAME, mUrl);
break;
}
} else {
mLoadId = mLoadMgr.reload(mLoadId, mLoadName, DEF_APP_NAME, mUrl);
}
}
*****
[Demo工程下载地址]
--http://git.oschina.net/jiake_xiaoyu/downloadmanager
[浏览器下载APP:参考]
--http://www.cnblogs.com/liyiran/p/6393813.html
[DownloadStatus]
http://blog.csdn.net/lonewolf521125/article/details/41477023
网友评论