VersionManagementUtil.java
package com.zozoms.rooexpress.utils;
import java.util.Arrays;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
public class VersionManagementUtil {
private static final String TAG = "VersionManagementUtil";
private static Context mContext;
private static final VersionManagementUtil INSTANCE = new VersionManagementUtil();
public static VersionManagementUtil getInstance(Context mContext) {
VersionManagementUtil.mContext = mContext;
return VersionManagementUtil.INSTANCE;
}
/**
* 获取版本号
*
* @return 当前应用的版本号,默认是1.0.0
*/
public static String getVersion(Context mContext) {
try {
PackageManager manager = mContext.getPackageManager();
PackageInfo info = manager.getPackageInfo(mContext.getPackageName(), 0);
String version = info.versionName;
return version;
} catch (Exception e) {
e.printStackTrace();
return "1.0.0";
}
}
/**
*
* @param version1
* @param version2
* @return if version1 > version2, return 1, if equal, return 0, else return
* -1
*/
public static int VersionComparison(String versionServer, String versionLocal) {
String version1 = versionServer;
String version2 = versionLocal;
if (version1 == null || version1.length() == 0 || version2 == null || version2.length() == 0)
throw new IllegalArgumentException("Invalid parameter!");
int index1 = 0;
int index2 = 0;
while (index1 < version1.length() && index2 < version2.length()) {
int[] number1 = getValue(version1, index1);
ZozoLog.i(TAG," ===== number1 ====" + Arrays.toString(number1));
int[] number2 = getValue(version2, index2);
ZozoLog.i(TAG," ===== number2 ====" + Arrays.toString(number2));
if (number1[0] < number2[0]){
ZozoLog.i(TAG," ===== number1[0] ====" + number1[0]);
ZozoLog.i(TAG," ===== number2[0] ====" + number2[0]);
return -1;
}
else if (number1[0] > number2[0]){
ZozoLog.i(TAG," ===== number1[0] ====" + number1[0]);
ZozoLog.i(TAG," ===== number2[0] ====" + number2[0]);
return 1;
}
else {
index1 = number1[1] + 1;
index2 = number2[1] + 1;
}
}
if (index1 == version1.length() && index2 == version2.length())
return 0;
if (index1 < version1.length())
return 1;
else
return -1;
}
/**
*
* @param version
* @param index
* the starting point
* @return the number between two dots, and the index of the dot
*/
public static int[] getValue(String version, int index) {
int[] value_index = new int[2];
StringBuilder sb = new StringBuilder();
while (index < version.length() && version.charAt(index) != '.') {
sb.append(version.charAt(index));
index++;
}
value_index[0] = Integer.parseInt(sb.toString());
value_index[1] = index;
return value_index;
}
public void Update(String url) {
ZozoLog.i(TAG, "Update:url = "+url);
UpDataManager updateManager = new UpDataManager(mContext);
updateManager.checkUpdateInfo(url);
}
}
调用
VersionResult versionResult = FastJsonTools.parseObject(new JSONObject(result).toString(),VersionResult.class);
String currentVersion = VersionManagementUtil.getVersion(mContext);
if(versionResult != null){
ZozoLog.d(TAG, "onHttpReqConnSuccess"+versionResult);
if(versionResult.getErrno() == 0){
if(VersionManagementUtil.VersionComparison(versionResult.getData().getVersion(),currentVersion)){
VersionManagementUtil.getInstance(mContext).Update(versionResult.getData().getDownload());
}else{
ToastUtils.showToast("当前已经是最新版本了");
}
}else{
ToastUtils.showToast(versionResult.getErrmsg());
}
}else{
ToastUtils.showToast("未能获取服务器版本号");
}