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);

}

}</

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值