阿里云服务器免费领卷啦。

捡代码论坛-最全的游戏源码下载技术网站!

 找回密码
 立 即 注 册

QQ登录

只需一步,快速开始

搜索
关于源码区的附件失效或欺骗帖, 处理办法
查看: 1810|回复: 0

一套完整的APP版本更新代码【原创】

[复制链接]

4208

主题

210

回帖

12万

积分

管理员

管理员

Rank: 9Rank: 9Rank: 9

积分
126084
QQ
发表于 2017-1-11 16:05:58 | 显示全部楼层 |阅读模式

在很多APP中版本更新是最基本的一个功能了,那么从检查版本更新到下载自动安装这一系列过程改如何实现呢,当然有很多方式,这里我总结了一种其中很基本的方式。

这是效果图:

pic-300x153.png


检查更新这个就不多说啦,这里我用的是通过OkHttpUtils网络请求框架来检测新版本。当然OkHttpUtils也有自己的下载功能,但是好像有很多限制,其中文件过大就会下载失败内存溢出等问题。

  1. public void checkUpdate(Activity activity, final boolean isShowTip) {
  2.     OkHttpUtils.get().url(Url.UPDATE_VERSION).build().execute(new StringCallback() {
  3.         @Override
  4.         public void onError(Call call, Exception e, int id) {

  5.         }

  6.         @Override
  7.         public void onResponse(String response, int id) {
  8.             final JsonData jsonData = JsonData.create(response);
  9.             int versionCode = jsonData.optInt("version");
  10.             int currentVersionCode = SystemUtils.getAppVersionCode(context);
  11.             if (versionCode > currentVersionCode) {
  12.                 AlertDialog.Builder builder = new AlertDialog.Builder(context);
  13.                 builder.setTitle("更新");
  14.                 builder.setMessage("检测到有更新,是否立刻更新?");
  15.                 builder.setNegativeButton("稍后更新", new DialogInterface.OnClickListener() {

  16.                     @Override
  17.                     public void onClick(DialogInterface dialog, int which) {
  18.                     }
  19.                 });
  20.                 builder.setPositiveButton("立刻更新", new DialogInterface.OnClickListener() {

  21.                     @Override
  22.                     public void onClick(DialogInterface dialog, int which) {
  23.                         if (!StringUtils.isWifi(context)) {
  24.                             AlertDialog.Builder builder = new AlertDialog.Builder(context);
  25.                             builder.setTitle("提示");
  26.                             builder.setMessage("您当前正在使用移动网络,继续下载将消耗流量");
  27.                             builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

  28.                                 @Override
  29.                                 public void onClick(DialogInterface dialog, int which) {

  30.                                 }
  31.                             });
  32.                             builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

  33.                                 @Override
  34.                                 public void onClick(DialogInterface dialog, int which) {
  35.                                     downLoadApk(jsonData.optString("download"));
  36.                                 }
  37.                             });
  38.                             builder.create().show();
  39.                         } else {
  40.                             downLoadApk(jsonData.optString("download"));
  41.                         }
  42.                     }
  43.                 });
  44.                 builder.create().show();
  45.             } else {
  46.                 if (isShowTip) {
  47.                     ToastUtils.show(context, "当前已是最新版本");
  48.                 }
  49.             }
  50.         }

  51.     });
  52. }

  53. /*
  54.    * 从服务器中下载APK
  55. */
  56.   protected void downLoadApk(final String url) {
  57.       final ProgressDialog pd;    //进度条对话框
  58.       pd = new ProgressDialog(this);
  59.       pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  60.       pd.setMessage("正在下载更新");
  61.       pd.show();
  62.       new Thread() {
  63.           @Override
  64.           public void run() {
  65.               try {
  66.                   File file = DownLoadManager.getFileFromServer(url, pd);
  67.                   sleep(3000);
  68.                   installApk(file);
  69.                   pd.dismiss(); //结束掉进度条对话框
  70.               } catch (Exception e) {
  71.                   Toast.makeText(context, "下载失败!",
  72.                           Toast.LENGTH_SHORT).show();
  73.                   e.printStackTrace();
  74.               }
  75.           }
  76.       }.start();
  77.   }

  78.   //安装apk
  79.   protected void installApk(File file) {
  80.       Intent intent = new Intent();
  81.       //执行动作
  82.       intent.setAction(Intent.ACTION_VIEW);
  83.       //执行的数据类型
  84.       intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
  85.       startActivity(intent);
  86.   }
复制代码

这里是下载数据的代码DownLoadManager.java

  1. import java.io.BufferedInputStream;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.InputStream;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;

  7. import android.app.ProgressDialog;
  8. import android.os.Environment;

  9. public class DownLoadManager {

  10.     public static File getFileFromServer(String path, ProgressDialog pd) throws Exception {
  11.         //如果相等的话表示当前的sdcard挂载在手机上并且是可用的
  12.         if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  13.             URL url = new URL(path);
  14.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  15.             conn.setConnectTimeout(5000);
  16.             //获取到文件的大小
  17.             pd.setMax(conn.getContentLength());
  18.             InputStream is = conn.getInputStream();
  19.             File file = new File(Environment.getExternalStorageDirectory(), "某某某.apk");
  20.             FileOutputStream fos = new FileOutputStream(file);
  21.             BufferedInputStream bis = new BufferedInputStream(is);
  22.             byte[] buffer = new byte[1024];
  23.             int len;
  24.             int total = 0;
  25.             while ((len = bis.read(buffer)) != -1) {
  26.                 fos.write(buffer, 0, len);
  27.                 total += len;
  28.                 //获取当前下载量
  29.                 pd.setProgress(total);
  30.             }
  31.             fos.close();
  32.             bis.close();
  33.             is.close();
  34.             return file;
  35.         } else {
  36.             return null;
  37.         }
  38.     }
  39. }
复制代码






捡代码论坛-最全的游戏源码下载技术网站! - 论坛版权郑重声明:
1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
2、本站所有主题由该帖子作者发表,该帖子作者与捡代码论坛-最全的游戏源码下载技术网站!享有帖子相关版权
3、捡代码论坛版权,详细了解请点击。
4、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,不存在任何商业目的与商业用途。
5、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。 我们不承担任何技术及版权问题,且不对任何资源负法律责任。
6、如无法链接失效或侵犯版权,请给我们来信:jiandaima@foxmail.com

回复

使用道具 举报

*滑块验证:
您需要登录后才可以回帖 登录 | 立 即 注 册

本版积分规则

技术支持
在线咨询
QQ咨询
3351529868

QQ|手机版|小黑屋|捡代码论坛-专业源码分享下载 ( 陕ICP备15015195号-1|网站地图

GMT+8, 2024-3-29 20:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表