一套完整的APP版本更新代码

男娘i 2022-06-15 02:44 491阅读 0赞

本文转载至IT985博客:点击打开链接

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

这是效果图:
pic

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

  1. publicvoid checkUpdate(Activity activity, finalboolean isShowTip) {
  2. OkHttpUtils.get().url(Url.UPDATE_VERSION).build().execute(newStringCallback() {
  3. @Override
  4. publicvoid onError(Call call, Exception e, intid) {
  5. }
  6. @Override
  7. publicvoid onResponse(String response, intid) {
  8. finalJsonData jsonData = JsonData.create(response);
  9. intversionCode = jsonData.optInt("version");
  10. intcurrentVersionCode = SystemUtils.getAppVersionCode(context);
  11. if(versionCode > currentVersionCode) {
  12. AlertDialog.Builder builder = newAlertDialog.Builder(context);
  13. builder.setTitle("更新");
  14. builder.setMessage("检测到有更新,是否立刻更新?");
  15. builder.setNegativeButton("稍后更新", newDialogInterface.OnClickListener() {
  16. @Override
  17. publicvoid onClick(DialogInterface dialog, intwhich) {
  18. }
  19. });
  20. builder.setPositiveButton("立刻更新", newDialogInterface.OnClickListener() {
  21. @Override
  22. publicvoid onClick(DialogInterface dialog, intwhich) {
  23. if(!StringUtils.isWifi(context)) {
  24. AlertDialog.Builder builder = newAlertDialog.Builder(context);
  25. builder.setTitle("提示");
  26. builder.setMessage("您当前正在使用移动网络,继续下载将消耗流量");
  27. builder.setNegativeButton("取消", newDialogInterface.OnClickListener() {
  28. @Override
  29. publicvoid onClick(DialogInterface dialog, intwhich) {
  30. }
  31. });
  32. builder.setPositiveButton("确定", newDialogInterface.OnClickListener() {
  33. @Override
  34. publicvoid onClick(DialogInterface dialog, intwhich) {
  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. protectedvoid downLoadApk(finalString url) {
  57. finalProgressDialog pd; //进度条对话框
  58. pd = newProgressDialog(this);
  59. pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  60. pd.setMessage("正在下载更新");
  61. pd.show();
  62. newThread() {
  63. @Override
  64. publicvoid 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. protectedvoid installApk(File file) {
  80. Intent intent = newIntent();
  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. importjava.io.BufferedInputStream;
  2. importjava.io.File;
  3. importjava.io.FileOutputStream;
  4. importjava.io.InputStream;
  5. importjava.net.HttpURLConnection;
  6. importjava.net.URL;
  7. importandroid.app.ProgressDialog;
  8. importandroid.os.Environment;
  9. publicclass DownLoadManager {
  10. publicstatic File getFileFromServer(String path, ProgressDialog pd) throwsException {
  11. //如果相等的话表示当前的sdcard挂载在手机上并且是可用的
  12. if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  13. URL url = newURL(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 = newFile(Environment.getExternalStorageDirectory(), "某某某.apk");
  20. FileOutputStream fos = newFileOutputStream(file);
  21. BufferedInputStream bis = newBufferedInputStream(is);
  22. byte[] buffer = newbyte[1024];
  23. intlen;
  24. inttotal = 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. returnfile;
  35. } else{
  36. returnnull;
  37. }
  38. }
  39. }

发表评论

表情:
评论列表 (有 0 条评论,491人围观)

还没有评论,来说两句吧...

相关阅读

    相关 完整SEO建站规则

    SEO已经成为网站建设的标配。一个好网站不仅要照顾到用户需求,还要符合搜索引擎规则,满足搜索引擎蜘蛛抓取、快照评级、提升关键词排序等。一个合格的优化师必须了解搜索引擎与网站的关