package com.jiuqi.njt.update; import org.json.JSONArray; import org.json.JSONObject; import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.os.AsyncTask; import com.jiuqi.njt.data.MyApp; import com.jiuqi.njt.data.OptsharepreInterface; import com.jiuqi.njt.ui.DownLoadApkTask; import com.jiuqi.njt.util.Constants; import com.jiuqi.njt.util.UIUtil; import com.jiuqi.njt.zxing.FinishListener; public class CheckVersionTask extends AsyncTask<Void, Integer, Boolean> { private static final String TAG = "Update"; private ProgressDialog pBar; private int newVerCode = 0; private String newVerName = ""; private Context currentContext; private int curVerCode; private boolean showProgress; private MyApp application; private int type ; //是否强制升级 public CheckVersionTask(Context context) { this(context, false); } public CheckVersionTask(Context context, boolean showProgress) { this.currentContext = context; this.curVerCode = Config.getVerCode(context); this.showProgress = showProgress; } private boolean getServerVerCode() { application = (MyApp) currentContext.getApplicationContext(); try { //FIXME 可能是这个地方报出来的错误 <html><head><title>Apache of type java.lang.String cannot be converted to JSONArray String verjson = NetworkTool.getContent(Config.UPDATE_SERVER + Config.UPDATE_VERJSON); JSONArray array = new JSONArray(verjson); if (array.length() > 0) { JSONObject obj = array.getJSONObject(0); try { newVerCode = UIUtil.tryToInteger(obj.getString("verCode")); newVerName = obj.getString("verName"); type = UIUtil.tryToInteger(obj.getString("type")); application.setNewVername(newVerName); } catch (Exception e) { newVerCode = -1; newVerName = ""; return false; } } } catch (Exception e) { if(e!=null){ //TODO 打印有异常 //Log.e(TAG, e.getMessage()); } return false; } return true; } private void doNewVersionUpdate() { int verCode = Config.getVerCode(currentContext); String verName = Config.getVerName(currentContext); String negativeStr = Constants.CANCEL_TEXT; if(1==type){ negativeStr = "退出"; } StringBuffer sb = new StringBuffer(); sb.append("当前版本:"); sb.append(verName); sb.append(",发现新版本"); sb.append(newVerName); sb.append("请下载更新"); AlertDialog d = new AlertDialog.Builder(currentContext) .setTitle("软件更新") .setCancelable(false) .setMessage(sb.toString()) .setPositiveButton("下载", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { new DownLoadApkTask(currentContext).execute(UIUtil.getNjtVo(currentContext)); } }) .setNegativeButton(negativeStr, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); if(1==type){ android.os.Process.killProcess(android.os.Process.myPid()); System.exit(0); } } }).create(); d.show(); } @Override protected void onPreExecute() { pBar = new ProgressDialog(currentContext); pBar.setTitle("版本升级"); pBar.setMessage("检查新版本.."); pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER); if (showProgress) { pBar.show(); } } @Override protected Boolean doInBackground(Void... params) { if (getServerVerCode()) { if (newVerCode > curVerCode) { return true; } } return false; } @Override protected void onPostExecute(Boolean result) { if(null != pBar){ pBar.dismiss(); } //记录当前的监测时间 OptsharepreInterface sharePre = new OptsharepreInterface(currentContext); sharePre.putPres("checkTime", String.valueOf(System.currentTimeMillis())); if (result) { if (showProgress){ doNewVersionUpdate(); }else{ application.setUpdate(true); } } else { if (showProgress) { String verName = Config.getVerName(currentContext); StringBuffer sb = new StringBuffer(); sb.append("当前版本:"); sb.append(verName.replace("ver:", "").replace("build", "日期")); sb.append(",\n已是最新版本,无需更新!"); Dialog dialog = new AlertDialog.Builder(currentContext) .setTitle("软件更新").setMessage(sb.toString()) .setPositiveButton("确定",null).create(); dialog.show(); }else{ application.setUpdate(false); } } } }