Android版本更新代码实现
16lz
2021-01-24
本文讲的是Android版本更新的实现代码
相关帮助文章:https://blog.csdn.net/JiYaRuo/article/details/106922141
//1.从后台获取版本信息实体 Version version = model.getModel(); //2.后台最新的版本号 int versionCode = Integer.valueOf(version.getVersionCode()); //3.当前版本< 后台最新版本 --> 更新 if (AppUtils.getVersionCode(getApplication()) < versionCode ) { showUpdate(version); } else { //直接进入主页面 } /** * 显示更新的dialog */ private void showUpdate(final Version version) { Dialog dialogUpdate = null; AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle("更新") .setMessage("发现新版本" + version.getVersionName() + (version.getOnlyAllowLatestAppVersion() == 1 ? " 请升级" : ",是否更新"))//.setMessage("发现新版本" + version.getVersionName() + " 请升级"+"\n"+"更新内容:"+"\n"+version.getInfo()) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { update(version); } }); dialogUpdate = builder.create(); dialogUpdate.setCancelable(false); dialogUpdate.show(); } /** * 下载安装包然后安装 * OkHttpUtils.getInstance().download : 安装的方法 */ private void update(Version version) { Tools.showWaitDialog(this, "下载中"); OkHttpUtils.getInstance().download(version.getDownLoadUrl(), new DownloadListener(FileUtils.getFilePath() + "/indentity", version.getVersionName() + ".apk") { @Override public void onUISuccess(File file) { if (file != null) { Tools.dismissWaitDialog(); //下载完成,开始安装 Uri uri = null; Intent intent = new Intent(Intent.ACTION_VIEW); try { if (Build.VERSION.SDK_INT >= 24) {//7.0 Android N //com.xxx.xxx.fileprovider为上述manifest中provider所配置相同 uri = FileProvider.getUriForFile(mContext, "自己的包名.fileprovider", file); intent.setAction(Intent.ACTION_INSTALL_PACKAGE); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//7.0以后,系统要求授予临时uri读取权限,安装完毕以后,系统会自动收回权限,该过程没有用户交互 intent.setDataAndType(uri, "application/vnd.android.package-archive"); startActivity(intent); } else { //7.0以下 AppUtils.installApk(getApplicationContext(), file);//storage/emulated/0/identity/file/indentity/2.0.7.apk } } catch (IllegalArgumentException e) { e.printStackTrace(); }catch (ActivityNotFoundException e){ e.printStackTrace(); }catch (Exception e){ e.printStackTrace(); } } else { ToastUtil.shortShowCustom(WelcomeActivity.this,"apk下载失败"); } } @Override public void onUIFailure(Exception e) { ToastUtil.shortShowCustom(WelcomeActivity.this,"下载失败"); Tools.dismissWaitDialog(); } @Override public void onUIProgress(Progress progress) { int ps = (int) (((float) progress.getCurrentBytes() / progress.getTotalBytes()) * 100); Tools.showWaitDialog(WelcomeActivity.this, "下载中" + (ps + "%")); } }); }
2.版本更新Bean类
package com.cmcc.app.entity;import java.io.Serializable;/** * Created by JiYaRuo. */public class Version implements Serializable { //版本名字 private String versionName; //版本号 private int versionCode; //是否强制更新 private int onlyAllowLatestAppVersion; //apk下载地址 private String downLoadUrl; //md5 private String md5; // 更新内容 private String info; //apk大小 private long apkSize; public String getVersionName() { return versionName; } public void setVersionName(String versionName) { this.versionName = versionName; } public int getVersionCode() { return versionCode; } public void setVersionCode(int versionCode) { this.versionCode = versionCode; } public int getOnlyAllowLatestAppVersion() { return onlyAllowLatestAppVersion; } public void setOnlyAllowLatestAppVersion(int onlyAllowLatestAppVersion) { this.onlyAllowLatestAppVersion = onlyAllowLatestAppVersion; } public String getDownLoadUrl() { return downLoadUrl; } public void setDownLoadUrl(String downLoadUrl) { this.downLoadUrl = downLoadUrl; } public void setInfo(String info) { this.info = info; } public String getInfo() { return info; }}
3.在清单文件中appliction下加入provider( 不加的话安卓7.0以上的手机安装完成后会闪退)
4.在res---xml---下新建文件,file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
这样就可以完成更新啦!
更多相关文章
- [Android]Android端ORM框架——RapidORM(v2.0)
- Android(安卓)Studio 自动检查依赖库是否有新版本
- 修正android cocos2dx项目当点击属性时提示错误的问题
- Windows7下使用Eclipse搭建Cocos2dx+Android开发环境
- AndroidStudio中的gradle脚本文件解读
- This Android(安卓)SDK requires Android(安卓)Developer Toolki
- android studio 4.0. gradle 4.0. tinker集成报错
- Ubuntun下Android(安卓)SDK环境搭建
- AES加解密在Android(安卓)4.2上的Encryption error