自动更新工具类

来源:互联网 发布:windows可上网镜像 编辑:程序博客网 时间:2024/06/16 08:22
public class DownLoadUtil {public static void checkNewVersion(final Activity context, final boolean isShowInfo) {RequestParams params = new RequestParams();params.put("PlatID", 1);params.put("Number", CommonUtil.getVersion(context));HttpUtil.asyncGet(API.URL_GET_APP_VERSION_INFO, params, new JsonHttpResponseHandler() {@Overridepublic void onStart() {// TODO Auto-generated method stubif (isShowInfo) {AlertUtil.showLoadingMessage(context);}}@Overridepublic void onSuccess(int statusCode, Header[] headers, final JSONObject response) {// TODO Auto-generated method stubif (!CommonUtil.checkStatus(response)) {if (isShowInfo) {AlertUtil.showErrorMessage(context, response.optString("Message"));}return;}if (response.optJSONObject("Result") != null) {showNewVersion(context, response);} else {if (isShowInfo) {AlertUtil.showInfoMessage(context, "已经是最新版本");}}}@Overridepublic void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {// TODO Auto-generated method stubif (isShowInfo) {AlertUtil.showErrorMessage(context, "服务器异常");}}@Overridepublic void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {// TODO Auto-generated method stubif (isShowInfo) {AlertUtil.showErrorMessage(context, "服务器异常");}}@Overridepublic void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {// TODO Auto-generated method stubif (isShowInfo) {AlertUtil.showErrorMessage(context, "服务器异常");}}@Overridepublic void onFinish() {// TODO Auto-generated method stubif (isShowInfo) {AlertUtil.dismiss(context);}}});}public static void showNewVersion(final Activity activity, final JSONObject response) {JSONObject result = response.optJSONObject("Result");if(result == null){return;}int enforceUpdate = result.optInt("EnforceUpdate");final String url = result.optString("DownloadUrl");if(enforceUpdate == 0){AlertUtil.AlertDialog(activity, "发现新版本可用,是否进行更新?", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubload(activity, url, dialog,response);}});}else{AlertUtil.AlertDialogPositive(activity, "提示", "发现新版本,需进行更新才可使用", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubload(activity, url, dialog,response);}});}}public static void load(final Activity activity, final String url, DialogInterface dialog, final JSONObject response) {dialog.dismiss();if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {activity.runOnUiThread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubAlertUtil.showErrorMessage(activity, "下载失败,SD卡不可用,下载失败");}});return;}final ProgressDialog pd = new ProgressDialog(activity);pd.setTitle("更新");pd.setMessage("正在下载");pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);pd.setCancelable(false);pd.setCanceledOnTouchOutside(false);pd.show();new Thread() {public void run() {File file = new File(Environment.getExternalStorageDirectory(),"guopu_" + System.currentTimeMillis() + ".apk");file = CommonUtil.download(pd, url, file.getAbsolutePath());if (file != null) {// 下载成功installApk(activity, file);} else {// 下载失败activity.runOnUiThread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubAlertUtil.showErrorMessage(activity, "下载失败",new android.view.View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubint enforceUpdate = response.optJSONObject("Result").optInt("EnforceUpdate");if(enforceUpdate == 1){showNewVersion(activity, response);}}});}});}pd.dismiss();}}.start();}/** * 安装一个应用程序 * * @param file */public static void installApk(Activity activity, File file) {Intent intent = new Intent();/* * <action android:name="android.intent.action.VIEW" /> <category * android:name="android.intent.category.DEFAULT" /> <data * android:scheme="content" /> <data android:scheme="file" /> <data * android:mimeType="application/vnd.android.package-archive" /> */intent.setAction("android.intent.action.VIEW");intent.addCategory("android.intent.category.DEFAULT");intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");activity.startActivity(intent);activity.finish();}}

0 0