#Android使用HttpUtils框架首页检查更新
检查本地Aapplication版本信息
/**
* 获取本地版本号和版本信息
*/
public static String getVersionName(Context context) {
String versionName = "";
try {
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
versionName=context.getPackageManager().getPackageInfo(context.getPackageName(),0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionName;
}
public static int getVersionCode(Context context) {
int versionCode = 0;
try {
versionCode=context.getPackageManager().getPackageInfo(context.getPackageName(),0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
###导入HttpUtils的jar包
下载Xutils.jar
流程图:
###HttpUtils请求服务器版本信息
//检查版本号
public void checkVersion() {
if (httpUtils == null)
httpUtils = new HttpUtils(2000);
httpUtils.send(HttpRequest.HttpMethod.GET, Constants.HOST + "/update.txt", new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
//String json = responseInfo.result;
//处理响应结果
handleResponse(responseInfo.result);
}
@Override
public void onFailure(HttpException e, String s) {
e.printStackTrace();
ToastUtils.showSafeToast(SplashActivity.this, "网络异常,请检查你的网络。。。");
turnToMain();
}
});
}
//处理请求结果
public void handleResponse(String json) {
UpdateInfo updateInfo = new UpdateInfo();
try {
//获取更新信息
JSONObject jsonObject = new JSONObject(json);
updateInfo.msg = jsonObject.getString("msg");
updateInfo.version = jsonObject.getInt("version");
updateInfo.versionName = jsonObject.getString("versionName");
updateInfo.downloadUrl = jsonObject.getString("downloadUrl");
int localVersion = PackageUtils.getVersionCode(SplashActivity.this);
if (localVersion < updateInfo.version) {
//旧版本,是否更新 对话框提示
showDialog(updateInfo.msg, Constants.HOST + updateInfo.downloadUrl);
} else {
//新版本 跳转到主页面
turnToMain();
}
} catch (Exception e) {
e.printStackTrace();
turnToMain();
}
}
//下载apk
private void downloadApk(String downloadUrl) {
if (httpUtils == null)
httpUtils = new HttpUtils(2000);
final ProgressDialog pd = new ProgressDialog(SplashActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setTitle("下载中...");
String target = Environment.getDataDirectory().getAbsolutePath() + "/new.apk";
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
target = Environment.getExternalStorageDirectory().getAbsolutePath() + "/new.apk";
}
final String apkPath = target;
boolean autoResume = false;//true:断点续传 如果下载完成再次下载会进入onFailure方法
httpUtils.download(downloadUrl, target, autoResume, new RequestCallBack<File>() {
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
//下载完成 掉用系统安装界面
installApk(apkPath);
pd.dismiss();
}
@Override
public void onFailure(HttpException e, String s) {
//下载失败
ToastUtils.showSafeToast(SplashActivity.this, "下载失败");
pd.dismiss();
turnToMain();
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
pd.setMax((int) total);
int percent = (int) (current * 100f / total);
pd.setProgress(percent);
}
});
}
###调用系统安装器安装apk
private void installApk(String path) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_INSTALL_PACKAGE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
String type = "application/vnd.android.package-archive";
intent.setDataAndType(Uri.parse("file://" + path), type);
//如果用户安装不用处理 如果用户不安装 跳转到主页面
startActivityForResult(intent, 0);
}
注解:[^note]:第一次写,不好之处还请各位指正,欢迎各位大神一起讨论交流与。欢迎界面以后可改用ViewPager,至于网络请求框架很多,LZ这里就不多说了。