public interface Callback { public void onSucc(); public void onFail(String msg); }
////////////////////////
@Override public void onClick(View v) {
this.getServerAppVersion(new Callback() {
@Override public void onSucc() {
new AlertDialog.Builder(MoreVersionActivity.this) .setTitle("版本更新检测") .setMessage("检测到新版本,是否下载更新?") .setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog, int which) { MoreVersionActivity.this .downloadApp(); } }).setNegativeButton("否", null) .setCancelable(false).show();
}
@Override public void onFail(String msg) { // Log.e("FindEventAddActivity", msg); }
});
}
public String getServerAppVersion(final Callback callback) {
RequestParams params = new RequestParams();
CommunicationTools.communication(Const.GET_SERVER_APP_VERSION_PATH, params, new JsonHttpResponseHandler() { @Override public void onSuccess(JSONObject data) { // 是否上传数据成功 try { String serverVersion = data.getString("versionNo");
String currentVsersion = MoreVersionActivity.this .getVersion();
if (serverVersion != null && serverVersion.contains("s")) { serverVersion = serverVersion.replace("s", ""); }
if (!serverVersion.equals(currentVsersion)) { callback.onSucc(); } else { PublicMethods.showToastTopShort( getApplicationContext(), "已经是最新版本!"); } } catch (JSONException e) { PublicMethods.showToastTopShort( getApplicationContext(), "网络连接失败!"); e.printStackTrace(); }
}
@Override public void onFailure(Throwable arg0) { PublicMethods.showToastTopShort( getApplicationContext(), "网络连接失败!"); callback.onFail("服务器连接失败"); } });
return null; }
///////////////////////////////////////////////////
new Thread(new Runnable() { @Override public void run() { Looper.prepare(); try { URL url = new URL(Const.GET_SERVER_APP_DOWNLOAD_PATH); HttpURLConnection urlConn = (HttpURLConnection) url .openConnection(); urlConn.setRequestProperty("Accept-Encoding", "identity"); urlConn.connect(); urlConn.setConnectTimeout(60 * 1000); InputStream is = urlConn.getInputStream(); Message msg = handler.obtainMessage(0); msg.obj = urlConn.getContentLength(); msg.sendToTarget(); downFile(is); } catch (Exception e) { e.printStackTrace(); // PublicMethods.showToastTopShort(getApplicationContext(), // "下载失败!"); new AlertDialog.Builder(MoreVersionActivity.this) .setTitle("提示").setMessage("下载失败,请检查网络是否正常,稍后重试!") .setPositiveButton("确定", null).setCancelable(false) .show(); } Looper.loop();
} }).start();