android version update

The whole idea is to first judge whether the current version of the server is higher than the local version. If it is higher, you can choose to update it. If there is no new version, it will prompt the user that the current version is the latest version
/**
* getVersionName (get the app version number)
*
* @Title: getVersionName
* @Description: TODO
* @param @param ct
* @param @return configuration file
* @return String return type
* @throws
*/
public static String getVersionName(Context ct) {
String versionName = null;
PackageManager packageManager = ct. getPackageManager();
try {
PackageInfo packInfo = packageManager.getPackageInfo(
ct.getPackageName(), 0);
versionName = packInfo.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return versionName;
}
/**
* isUpdate(是否更新)
*
* @Title: isUpdate
* @Description: TODO
* @param @param oldVersionName
* @param @param newVersionName
* @param @return 设定文件
* @return boolean 返回类型
* @throws
*/
public static boolean isUpdate(String newVersionName, String oldVersionName) {
boolean result = false;
try {
String n[] = newVersionName.split("\\.");
String o[] = oldVersionName.split("\\.");
for (int i = 0; i < n.length; i++) {
if (Integer.parseInt(n[i]) > Integer.parseInt(o[i])) {
return true;
}
if (Integer.parseInt(n[i]) < Integer.parseInt(o[i])) {
return false;
}

}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
这里用xutils做的网络请求
public void checkUpdate() {
new HttpUtils().send(HttpMethod.POST, RequestPath.ACTION_UPDATE,
new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {

}

@Override
public void onSuccess(ResponseInfo<String> arg0) {
try {
JSONObject jsonObject = new JSONObject(arg0.result);
if (jsonObject.getInt("code") == 1) {
Gson gson = new Gson();
final VersionUpdateDTO version = gson.fromJson(
jsonObject.getJSONObject("message")
.toString(),
VersionUpdateDTO.class);
String oldVersion = Utils
.getVersionName(ctx);
if (Utils.isUpdate(version.getBanbenNum(),
oldVersion)) {
update(version.getDownLoadUrl());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}

private AppUpdateDialog appUpdateDialog;
private ProgressBar progressBar;
private TextView progressText;

/**
* @param url
*            更新APP
*/
public void update(String url) {
String target = Utils.getSDPath() + "/download";
String log = Utils.getSDPath() + "/log";
File file = new File(target);
File fileLog = new File(log);
if (fileLog.exists()) {
Utils.deleteFile(fileLog);
}
if (!file.exists()) {
file.mkdirs();
} else {
Utils.deleteFile(file);
file.mkdirs();
}
target = target + "/" + Utils.getFileName(url);
if (appUpdateDialog == null) {
appUpdateDialog = new AppUpdateDialog(ctx);
progressBar = (ProgressBar) appUpdateDialog
.findViewById(R.id.progress);
progressText = (TextView) appUpdateDialog
.findViewById(R.id.progressText);
}
try {
appUpdateDialog.show();
} catch (Exception e) {
e.printStackTrace();
}
new HttpUtils().download(url, target, true, true,
new RequestCallBack<File>() {

@Override
public void onStart() {
super.onStart();
DebugTools.d("开始下载");
}

@Override
public void onLoading(long total, long current,
boolean isUploading) {
super.onLoading(total, current, isUploading);
progressBar.setMax((int) total);
progressBar.setProgress((int) current);
progressText.setText("正在更新"
+ String.valueOf((int) (((double) current / total) * 100))
+ "%");
}

@Override
public void onSuccess(ResponseInfo<File> arg0) {
appUpdateDialog.dismiss();
File file = arg0.result;
Utils.install(ctx, file.toString());
}

@Override
public void onFailure(HttpException arg0, String arg1) {
if (appUpdateDialog != null) {
if (appUpdateDialog.isShowing()) {
appUpdateDialog.dismiss();
}
}
UIHelper.toastBar(ctx, "更新失败!errorcode="
+ arg0.getExceptionCode());
}
});
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326768966&siteId=291194637