import com.devilwwj.updateapkdemo.utils.Utils;
import com.loopj.android.http.JsonHttpResponseHandler;
import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Map;
/**
-
检查更新的句柄
-
@author wwj_748
*/
public class CheckUpdateAsyncHandler extends JsonHttpResponseHandler{
private Context mContext;
private ProgressDialog progressDialog;
OnCheckUpdateListener checkUpdateListener;
interface OnCheckUpdateListener {
void onSuccess(UpdateInfo updateInfo);
void onFailure();
}
public CheckUpdateAsyncHandler(Context context, OnCheckUpdateListener onCheckUpdateListener) {
this.mContext = context;
this.checkUpdateListener = onCheckUpdateListener;
progressDialog = new ProgressDialog(mContext);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage(mContext.getString(R.string.tip_requesting));
progressDialog.setCancelable(true);
}
@Override
public void onStart() {
super.onStart();
progressDialog.show();
}
@Override
public void onFailure(int statusCode, Header[] headers,
Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
Map<String, Object> versionInfoMap = GlobalSettings
.getNewVersionInfo(mContext);
boolean canUpdate = (Boolean) versionInfoMap.get(“updatable”);
if (canUpdate) {
progressDialog.dismiss();
Toast.makeText(mContext, “连接失败,请稍后重试”,
Toast.LENGTH_SHORT).show();
} else {
progressDialog.dismiss();
}
checkUpdateListener.onFailure();
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
Log.d(“updateInfo”, response.toString());
try {
JSONObject firstObject = (JSONObject) response.get(“android_ireader”);
// 获取日志更新配置
int frequency = firstObject.optInt(“frequency”);
int sendLogStatus = firstObject.optInt(“send_log”);
// 最新版本号
String versionName = firstObject.optString(“app_version”);
// 版本号
int versionCode = firstObject.optInt(“version_code”);
versionCode = 47;
// 更新内容
String features = firstObject.getString(“features”);
// sdk版本
String sdkVersion = firstObject.optString(“sdk_version”);
// 系统版本
String osVersion = firstObject.optString(“os_version”);
// 获取当前版本号
int currentVersionCode = Utils.getAppVersionCode(mContext);
// apk下载地址
String url = firstObject.optString(“update_url”);
boolean isCanUpdate;
// 获取上次强制更新的版本——last_force_update
int last_force_update_version = firstObject.optInt(“last_force_update_version”);
if (currentVersionCode < last_force_update_version) {
isCanUpdate = true;
} else {
if (ver
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
sionCode <= currentVersionCode) {
isCanUpdate = false;
} else {
isCanUpdate = true;
}
}
// 保存更新信息到sharedPerences
GlobalSettings.saveNewVersionInfo(mContext, versionName, url, features, isCanUpdate);
UpdateInfo updateInfo = new UpdateInfo();
updateInfo.setVersionName(versionName);
updateInfo.setVersionCode(versionCode);
updateInfo.setFeatures(features);
updateInfo.setSdkVersion(sdkVersion);
updateInfo.setOsVersion(osVersion);
updateInfo.setUpdateUrl(url);
updateInfo.setLastForceUpdate(last_force_update_version);
checkUpdateListener.onSuccess(updateInfo);
} catch(JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFinish() {
super.onFinish();
progressDialog.dismiss();
}
@Override
public void onCancel() {
super.onCancel();
progressDialog.cancel();
}
}
定义更新管理类
package com.devilwwj.update.http;
import android.content.Context;
/**
-
com.devilwwj.update.http
-
Created by devilwwj on 16/4/5.
*/
public class AppUpdateManager {
private Context mContext;
private static AppUpdateManager instance;
public static AppUpdateManager getInstance(Context context) {
if (instance == null) {
synchronized (AppUpdateManager.class) {
instance = new AppUpdateManager(context);
}
}
return instance;
}
public AppUpdateManager(Context context) {
this.mContext = context;
}
public void checkUpdate() {
HttpRequestHelper.getInstance().checkUpdate(null, new CheckUpdateAsyncHandler(mContext))
}