`

android端版本更新

阅读更多
整个思路,先判断服务端当前版本是不是高于本地版本,高的话可以选择更新,如果没有新版本,则提示用户当前是最新版本
/**
* getVersionName(获取app版本号)
*
* @Title: getVersionName
* @Description: TODO
* @param @param ct
* @param @return 设定文件
* @return String 返回类型
* @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());
}
});
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics