public void getInfo() {
HttpUtils httpUtils = new HttpUtils();
HttpHandler<String> send = httpUtils.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
String result = responseInfo.result.toString();
Log.d("print", result);
Gson gson = new Gson();
bean = gson.fromJson(result, Bean.class);
if (bean.getVerCode() > getVerCode()) {
//更新新的版本
doUpate();
} else {
//提示当前版本就是最新的版本
}
Log.d("print", "onSuccess" + result);
}
@Override
public void onFailure(HttpException e, String s) {
Log.d("print", "onFailure" + s);
}
});
}
//进行判断和更新
public void doUpate() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("当前版本:" + getVerName() + "VerCode" + getVerCode() + ",");
stringBuffer.append("发现新版本:" + bean.getVerName() + "VerCode" + bean.getVerCode());
stringBuffer.append("是否更新?");
Dialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("软件更新").setMessage(stringBuffer.toString())
// 设置内容
.setPositiveButton("更新",// 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
//下载新版本的应用
downLoad();
}
})
.setNegativeButton("暂不更新",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 点击"取消"按钮之后退出程序
finish();
}
}).create();// 创建
// 显示对话框
dialog.show();
;
}
public void downLoad() {
HttpUtils http = new HttpUtils();
HttpHandler handler = http.download(
bean.getApkpath(),
"/sdcard/fileexplorer.apk",
true, // 如果目标文件存在,接着未完成的部分继续下载。
true, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。
new RequestCallBack<File>() {
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
Log.d("print", "下载成功!");
File result = responseInfo.result;
installApk(result.getPath());
}
@Override
public void onFailure(HttpException e, String s) {
Log.d("print", "下载失败!" + s);
}
}
);
}
5.安装下载的APK
//安装
public void installApk(String apkFilePath) {
File apkfile = new File(apkFilePath);
if (!apkfile.exists()) {
Log.d("print", "路径不存在....");
} else {
Intent i = new Intent("android.intent.action.VIEW");
i.addFlags(268435456);
i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "applicationnd.android.package-archive");
startActivity(i);
}
}