Android实战之app版本更新升级全文章(二)(2)

当然啦,前提是你得依赖了我的base库:

https://github.com/LuoGuoXin/BaseAndroid

哈哈,你也可以直接复制版本更新的部分过去就行啦,再根据自己需求修改下,有什么好的建议记得评论,因为那个功能我也还没去优化的。

那下面进行代码分析哈:首先是主方法

/**

  • 版本更新

  • @param context

  • @param versionCode 版本号

  • @param url apk下载地址

  • @param updateMessage 更新内容

  • @param isForced 是否强制更新

*/

public static void checkUpdate(Context context, int versionCode, String url, String updateMessage, boolean isForced) {

if (versionCode > UpdateManager.getInstance().getVersionCode(context)) {

int type = 0;//更新方式,0:引导更新,1:安装更新,2:强制更新

if (UpdateManager.getInstance().isWifi(context)) {

type = 1;

}

if (isForced) {

type = 2;

}

//检测是否已下载

String downLoadPath = Environment.getExternalStorageDirectory().getAbsolutePath() + “/downloads/”;

File dir = new File(downLoadPath);

if (!dir.exists()) {

dir.mkdir();

}

String fileName = url.substring(url.lastIndexOf(“/”) + 1, url.length());

if (fileName == null && TextUtils.isEmpty(fileName) && !fileName.contains(“.apk”)) {

fileName = context.getPackageName() + “.apk”;

}

File file = new File(downLoadPath + fileName);

//设置参数

UpdateManager.getInstance().setType(type).setUrl(url).setUpdateMessage(updateMessage).setFileName(fileName).setIsDownload(file.exists());

if (type == 1 && !file.exists()) {

UpdateManager.getInstance().downloadFile(context);

} else {

UpdateManager.getInstance().showDialog(context);

}

}

}

然后就是UpdateManager的封装啦,自己看代码吧,有什么优化点评论下,我去修改哈。
/** * 版本更新 / public class UpdateManager { private String downLoadPath = Environment.getExternalStorageDirectory().getAbsolutePath() + “/downloads/”; private int type = 0;//更新方式,0:引导更新,1:安装更新,2:强制更新 private String url = “”;//apk下载地址 private String updateMessage = “”;//更新内容 private String fileName = null;//文件名 private boolean isDownload = false;//是否下载 private NotificationManager mNotifyManager; private NotificationCompat.Builder mBuilder; private BaseDialog dialog; private ProgressDialog progressDialog; public static UpdateManager updateManager; public static UpdateManager getInstance() { if (updateManager == null) { updateManager = new UpdateManager(); } return updateManager; } private UpdateManager() { } /* * 弹出版本更新提示框 / public void showDialog(final Context context) { String title = “”; String left = “”; boolean cancelable = true; if (type == 1 | isDownload) { title = “安装新版本”; left = “立即安装”; } else { title = “发现新版本”; left = “立即更新”; } if (type == 2) { cancelable = false; } dialog = new BaseDialog.Builder(context).setTitle(title).setMessage(updateMessage).setCancelable(cancelable) .setLeftClick(left, new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); if (type == 1 | isDownload) { installApk(context, new File(downLoadPath, fileName)); } else { if (url != null && !TextUtils.isEmpty(url)) { if (type == 2) { createProgress(context); } else { createNotification(context); } downloadFile(context); } else { Toast.makeText(context, “下载地址错误”, Toast.LENGTH_SHORT).show(); } } } }) .setRightClick(“取消”, new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); if (type == 2) { System.exit(0); } } }) .create(); dialog.show(); } /* * 下载apk * / public void downloadFile(final Context context) { RequestParams params = new RequestParams(url); params.setSaveFilePath(downLoadPath + fileName); x.http().request(HttpMethod.GET, params, new Callback.ProgressCallback() { @Override public void onSuccess(File result) { } @Override public void onError(Throwable ex, boolean isOnCallback) { Toast.makeText(context, ex.getMessage(), Toast.LENGTH_SHORT).show(); } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } @Override public void onWaiting() { } @Override public void onStarted() { } @Override public void onLoading(long total, long current, boolean isDownloading) { //实时更新通知栏进度条 if (type == 0) { notifyNotification(current, total); } else if (type == 2) { progressDialog.setProgress((int) (current * 100 / total)); } if (total == current) { if (type == 0) { mBuilder.setContentText(“下载完成”); mNotifyManager.notify(10086, mBuilder.build()); } else if (type == 2) { progressDialog.setMessage(“下载完成”); } if (type == 1) { showDialog(context); } else { installApk(context, new File(downLoadPath, fileName)); } } } }); } /* * 强制更新时显示在屏幕的进度条 * / private void createProgress(Context context) { progressDialog = new ProgressDialog(context); progressDialog.setMax(100); progressDialog.setCancelable(false); progressDialog.setMessage(“正在下载…”); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.show(); } /* * 创建通知栏进度条 * / private void createNotification(Context context) { mNotifyManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); mBuilder = new NotificationCompat.Builder(context); mBuilder.setSmallIcon(BaseAndroid.getBaseConfig().getAppLogo()); mBuilder.setContentTitle(“版本更新”); mBuilder.setContentText(“正在下载…”); mBuilder.setProgress(0, 0, false); Notification notification = mBuilder.build(); notification.flags = Notification.FLAG_AUTO_CANCEL; mNotifyManager.notify(10086, notification); } /* * 更新通知栏进度条 * / private void notifyNotification(long percent, long length) { mBuilder.setProgress((int) length, (int) percent, false); mNotifyManager.notify(10086, mBuilder.build()); } /* * 安装apk * * @param context 上下文 * @param file APK文件 / private void installApk(Context context, File file) { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), “application/vnd.android.package-archive”); context.startActivity(intent); } /* * @return 当前应用的版本号 / public int getVersionCode(Context context) { try { PackageManager manager = context.getPackageManager(); PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0); int version = info.versionCode; return version; } catch (Exception e) { e.printStackTrace(); return 0; } } /* * 判断当前网络是否wifi */ public boolean isWifi(Context mContext) { ConnectivityManager connectivityManager = (ConnectivityManager) mContext .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) { return true; } return false; } public UpdateManager setUrl(String url) { this.url = url; return this; } public UpdateManager setType(int type) { this.type = type; return this; } public UpdateManager setUpdateMessage(String updateMessage) { this.updateMessage = updateMessage; return this; } public UpdateManager setFileName(String fileName) { this.fileName = fileName; return this; } public UpdateManager setIsDownload(boolean download) { isDownload = download; return this; } }

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

总结

找工作是个很辛苦的事情,而且一般周期都比较长,有时候既看个人技术,也看运气。第一次找工作,最后的结果虽然不尽如人意,不过收获远比offer大。接下来就是针对自己的不足,好好努力了。

最后为了节约大家的时间,我把我学习所用的资料和面试遇到的问题和答案都整理成了PDF文档

喜欢文章的话请关注、点赞、转发 谢谢!

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

803510506)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值