Android 手机卫士(8)下载服务器上的APK
2017-09-18 19:24
183 查看
// 下载apk,用到了xutils架包(需要导入架包置于工程的lib目录下) // xutils的架包可以自行x度下载,文章末尾提供了个人使用的架包地址. // xutils的注意事项: // (1)不光lib下有该架包,还需要在Android Private Libraries下也存在[这个可能会帮你自动做] // (2)配置清单文件里面加上网络和读取外部存储的权限[xutils一共就需要这两个权限] // <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> // <uses-permission android:name="android.permission.INTERNET" /> // 使用: // 获取HttpUtils对象,下载指定链接地址的APK downLoadApk();//见"五.downLoadApk()具体实现与讲解"
一.什么是xutils?
xUtils 包含了很多实用的android工具。
xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(10种谓词),拥有更加灵活的ORM,更多的事件注解支持且不受混淆影响……
xUitls最低兼容Android 2.2 (API Level 8)。
二.xUtils的四大模块
(1)DbUtils模块
android中的orm框架,一行代码就可以进行增删改查; 支持事务,默认关闭; 可通过注解自定义表名,列名,外键,唯一性约束,NOT NULL约束,CHECK约束等(需要混淆的时候请注解表名和列名); 支持绑定外键,保存实体时外键关联实体自动保存或更新; 自动加载外键关联实体,支持延时加载; 支持链式表达查询,更直观的查询语义,参考下面的介绍或sample中的例子。
(2)ViewUtils模块
android中的ioc框架,完全注解方式就可以进行UI,资源和事件绑定; 新的事件绑定方式,使用混淆工具混淆后仍可正常工作; 目前支持常用的20种事件绑定,参见ViewCommonEventListener类和包com.lidroid.xutils.view.annotation.event。
(3)HttpUtils模块
支持同步,异步方式的请求; 支持大文件上传,上传大文件不会oom; 支持GET,POST,PUT,MOVE,COPY,DELETE,HEAD,OPTIONS,TRACE,CONNECT请求; 下载支持301/302重定向,支持设置是否根据Content-Disposition重命名下载的文件; 返回文本内容的请求(默认只启用了GET请求)支持缓存,可设置默认过期时间和针对当前请求的过期时间。
(4)BitmapUtils模块
加载bitmap的时候无需考虑bitmap加载过程中出现的oom和android容器快速滑动时候出现的图片错位等现象; 支持加载网络图片和本地图片; 内存管理使用lru算法,更好的管理bitmap内存; 可配置线程加载线程数量,缓存大小,缓存路径,加载显示动画等...
三.xUtils快速开发框架需要有以下权限
android:name="android.permission.INTERNET" android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
四.HttpUtils模块的使用方法[我们需要使用的就是这个模块]
(1)普通get方法
HttpUtils http = new HttpUtils(); http.send(HttpRequest.HttpMethod.GET, "http://www.lidroid.com", new RequestCallBack<String>(){ @Override public void onLoading(long total, long current, boolean isUploading) { testTextView.setText(current + "/" + total); } @Override public void onSuccess(ResponseInfo<String> responseInfo) { textView.setText(responseInfo.result); } @Override public void onStart() { } @Override public void onFailure(HttpException error, String msg) { } });
(2)上传文件或者提交数据 到服务器(post方法)
RequestParams params = new RequestParams(); params.addHeader("name", "value"); params.addQueryStringParameter("name", "value"); // 只包含字符串参数时默认使用BodyParamsEntity, // 类似于UrlEncodedFormEntity("application/x-www-form-urlencoded")。 params.addBodyParameter("name", "value"); // 加入文件参数后默认使用MultipartEntity("multipart/form-data"), // 如需"multipart/related",xUtils中提供的MultipartEntity支持设置subType为"related"。 // 使用params.setBodyEntity(httpEntity)可设置更多类型的HttpEntity(如: // MultipartEntity,BodyParamsEntity,FileUploadEntity,InputStreamUploadEntity,StringEntity)。 // 例如发送json参数:params.setBodyEntity(new StringEntity(jsonStr,charset)); params.addBodyParameter("file", new File("path")); ... HttpUtils http = new HttpUtils(); http.send(HttpRequest.HttpMethod.POST, "uploadUrl....", params, new RequestCallBack<String>() { @Override public void onStart() { testTextView.setText("conn..."); } @Override public void onLoading(long total, long current, boolean isUploading) { if (isUploading) { testTextView.setText("upload: " + current + "/" + total); } else { testTextView.setText("reply: " + current + "/" + total); } } @Override public void onSuccess(ResponseInfo<String> responseInfo) { testTextView.setText("reply: " + responseInfo.result); } @Override public void onFailure(HttpException error, String msg) { testTextView.setText(error.getExceptionCode() + ":" + msg); } });
(3)下载文件
HttpUtils http = new HttpUtils(); HttpHandler handler = http.download("http://apache.dataguru.cn/httpcomponents/httpclient/source/httpcomponents-client-4.2.5-src.zip", "/sdcard/httpcomponents-client-4.2.5-src.zip", true, // 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。 true, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。 new RequestCallBack<File>() { @Override public void onStart() { testTextView.setText("conn..."); } @Override public void onLoading(long total, long current, boolean isUploading) { testTextView.setText(current + "/" + total); } @Override public void onSuccess(ResponseInfo<File> responseInfo) { testTextView.setText("downloaded:" + responseInfo.result.getPath()); } @Override public void onFailure(HttpException error, String msg) { testTextView.setText(msg); } }); ... //调用cancel()方法停止下载 handler.cancel();
五.downLoadApk()具体实现与讲解
// (1)sd许要判断是否可用(挂载与否) if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { // (2)apk下载到sd卡路径的获取设置 String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "SafeManager.apk"; // (3)发送请求,获取apk,并且放置在指定的位置 HttpUtils httpUtils = new HttpUtils(); httpUtils.download(mdownloadUrl, path, new RequestCallBack<File>() { @Override public void onStart() { super.onStart(); ToastUtil.show(SplashActivity.this, "DownLoad Start"); } @Override public void onLoading(long total, long current, boolean isUploading) { // 参数介绍:apk的总大小,当前的下载位置,是否正在下载 super.onLoading(total, current, isUploading); ToastUtil.show(SplashActivity.this, "DownLoading"); Log.d(tag, "" + total); Log.d(tag, "" + current); Log.d(tag, "" + isUploading); } @Override public void onSuccess(ResponseInfo<File> arg0) { // 下载过后放置sd卡的apk ToastUtil.show(SplashActivity.this, "DownLoad Success"); File file = arg0.result; // 提示用户安装 // installApk(file); } @Override public void onFailure(HttpException arg0, String arg1) { ToastUtil.show(SplashActivity.this, "DownLoad Failure"); } }); }
解析: HttpHandler<File> com.lidroid.xutils.HttpUtils.download(String url, String target, RequestCallBack<File> callback) url:服务器端apk的url target:下载到本地的绝对路径 callback:RequestCallBack对象
就到这里啦!
主要讲述的是运用xutils框架实现下载文件的功能!
xutils下载地址:【稍后贴出】
よろしくお願いします。
相关文章推荐
- APK 变成 ZIP:Android 手机应用程序文件下载服务器需要注意的问题
- APK 变成 ZIP:Android 手机应用程序文件下载服务器需要注意的问题
- APK 变成 ZIP:Android 手机应用程序文件下载服务器需要注意的问题
- 解决IE下载 apk/ipa 变成zip:Android 手机应用程序文件下载服务器 配置解决方法
- APK 变成 ZIP:Android 手机应用程序文件下载服务器需要注意的问题
- APK 变成 ZIP:Android 手机应用程序文件下载服务器需要注意的问题
- Android 手机卫士--打包生成apk维护到服务器
- APK 变成 ZIP:Android 手机应用程序文件下载服务器需要注意的问题
- APK 变成 ZIP:Android 手机应用程序文件下载服务器需要注意的问题
- android 从服务器上获取APK下载安装
- Android 手机卫士--xutils说明与下载方法使用
- iis服务器设置Android apk下载的MIME 设置
- android 从服务器上获取APK下载安装
- android 默认浏览器 无法下载,此手机不支持此内容(自定义文件or APK文件看过了)
- Android 中tomcat搭建本地服务器 实现apk更新下载
- android 从服务器上获取APK下载安装
- Android 手机卫士(5)从Apache Tomcat服务器获取数据并解析
- Android 中tomcat搭建本地服务器 实现apk更新下载
- Android - 代码的组织方式,splash界面,获取应用的版本号,获取服务器版本号,下载安装apk,跑马灯效果
- android 从服务器上获取APK下载安装