本文主要是介绍Android APP更新下载工具类——简单封装DownloadManager,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
几乎所有APP都包含了检查更新功能,更新下载功能的实现方式常用的有两种:1、使用App网络框架的文件下载请求;2、使用自带的DownloadManager类;本文介绍第二种,简单封装一下DownloadManager进行下载更新。重要声明:本文严格意义上来说并非原创,参考了网上一些文章,他们写的都很好,博主只是按照自己的习惯再次封装一遍,方便自己使用,在此发出只供大家分享指教
参考文章:CSDN——http://blog.csdn.net/sir_zeng/article/details/8983430
简书——http://www.jianshu.com/p/7ad92b3d9069
正文:
在此不对DownloadManager做解释,请参考上边两篇文章
1、初始化
DownloadManager downloadManager;
/*** 初始化downloadManager*/
private void getService() {String serviceString = Context.DOWNLOAD_SERVICE;downloadManager = (DownloadManager) context.getSystemService(serviceString);
}
2、发起下载任务,主要是对DownloadManager.Request request 对象进行配置
List<Long> downloadIds;//记录所有下载任务id/** * @param uil 下载地址 * @param title 通知栏标题* @param description 描述* @return */
public long download(String uil, String title, String description) {Uri uri = Uri.parse(uil);DownloadManager.Request request = new DownloadManager.Request(uri);//设置下载地址request.setTitle(title);//设置Notification的title信息request.setDescription(description);//设置Notification的message信息request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);//设置通知栏下载通知显示状态//下载到download文件夹下 request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "update.apk");//只能在WiFi下进行下载request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);//reference变量是系统为当前的下载请求分配的一个唯一的ID,我们可以通过这个ID重新获得这个下载任务,进行一些自己想要进行的操作或者查询long id = downloadManager.enqueue(request);downloadIds.add(id);return id;
}
3、辅助方法 判断下载组件是否可以 和 开启下载组件
/*** 判断下载组件是否可用** @param context* @return*/private boolean canDownloadState(Context context) {try {int state = context.getPackageManager().getApplicationEnabledSetting("com.android.providers.downloads");if (state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED|| state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER|| state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {return false;}} catch (Exception e) {e.printStackTrace();return false;}return true;}/*** 启用下载组件** @param context*/private void enableDowaload(Context context) {String packageName = "com.android.providers.downloads";Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);intent.setData(Uri.parse("package:" + packageName));context.startActivity(intent);}
4、下载完成监听
(1)定义下载完成监听接口
/*** 下载完成监听*/public interface OnDownloadCompleted {public void onDownloadCompleted(long completeDownloadId);}
(2)继承BroadcastReceiver监听下载完成消息
OnDownloadCompleted onDownloadCompleted;
/*** 下载完成监听*/
class DownloadCompleteReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {long completeDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);//获取下载完成任务的idif (onDownloadCompleted != null) {onDownloadCompleted.onDownloadCompleted(completeDownloadId);//调用下载完成接口方法}//下载完成后删除idfor (int i = 0; i < downloadIds.size(); i++) {if (completeDownloadId == downloadIds.get(i)) {downloadIds.remove(i);}}}
}
(3)在Activity或Service中注册DownloadBroadcast广播和监听接口
DownloadCompleteReceiver downLoadCompleteReceiver;/*** 注册广播并注册监听接口** @param onDownloadCompleted*/
public void registerReceiver(OnDownloadCompleted onDownloadCompleted) {context.registerReceiver(downLoadCompleteReceiver,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));this.onDownloadCompleted = onDownloadCompleted;
}
(4)使用结束后记得解除DownloadCompleteReceiver广播的注册
/*** 解除注册广播*/public void unregisterReceiver() {context.unregisterReceiver(downLoadCompleteReceiver);}
5、获取下载完成的apk文件并安装
/*** 根据任务id打开安装界面** @param downloadApkId*/public void installApk(long downloadApkId) {Intent install = new Intent(Intent.ACTION_VIEW);Uri downloadFileUri = downloadManager.getUriForDownloadedFile(downloadApkId);//获取下载文件路径if (downloadFileUri != null) {install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);try {context.startActivity(install);//打开安装界面} catch (ActivityNotFoundException ex) {ex.printStackTrace();}}}
6、获取所有任务id
/*** 获取当前下载的所有任务 id** @return*/public List<Long> getDownloadIds() {return downloadIds;}
8、构造方法
public DownloadManagerUtils(Context context) {this.context = context;getService();downLoadCompleteReceiver = new DownloadCompleteReceiver();downloadIds = new ArrayList<Long>();}
9、使用
DownloadManagerUtils downloadManagerUtils;private void testDownload(){downloadManagerUtils = new DownloadManagerUtils(this);long id = downloadManagerUtils.download("http://www.apk3.com/uploads/soft/guiguangbao/txsjgj.apk","测试下载","这是一次测试下载");downloadManagerUtils.registerReceiver(new DownloadManagerUtils.OnDownloadCompleted() {//注册广播和下载完成监听@Overridepublic void onDownloadCompleted(long completeDownloadId) {downloadManagerUtils.installApk(completeDownloadId);//调用第5步的方法}});}@Overrideprotected void onDestroy() {downloadManagerUtils.unregisterReceiver();//销毁是解除注册super.onDestroy();}
10、总结:
本文对于DownloadManager的应用智能使用与app更新下载,当然DownloadManager是可以下载各种文件的,具体请参考文章开始两篇文章。
结尾:本文只是个人观点,文章中如有不妥之处敬请留言指教。
这篇关于Android APP更新下载工具类——简单封装DownloadManager的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!