当然啦,前提是你得依赖了我的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);
}
}</