private void checkUpdate() { new Thread() { public void run() { long currentTime = System.currentTimeMillis(); Message msg = Message.obtain(); try { URL url = new URL(getString(R.string.serverurl)); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(4000); int code = conn.getResponseCode(); if (code == 200) { // 联网成功 InputStream is = conn.getInputStream(); String infoString = StreamTools.readFromStream(is); // System.out.println("联网成功--------->"+infoString); JSONObject jsonObject = new JSONObject(infoString); String version = jsonObject.optString("version"); description = jsonObject.optString("description"); apkurl = jsonObject.optString("apkurl"); System.out.println("----->" + version + description + apkurl); // 校验是否有新版本 if (getVersionName().equals(version)) { // 版本一致 进入主页面 msg.what = GOTO_HOME; } else { // 更新版本 弹出对话框提示 msg.what = UPDATE_VERSION; } } } catch (MalformedURLException e) { msg.what = URL_ERROR; e.printStackTrace(); } catch (IOException e) { msg.what = IO_ERROR; e.printStackTrace(); } catch (JSONException e) { msg.what = JSON_ERROR; e.printStackTrace(); } finally { long endTime = System.currentTimeMillis(); long time = currentTime - endTime; if (time < 2000) { try { Thread.sleep(2000 - time); } catch (InterruptedException e) { e.printStackTrace(); } } handler.sendMessage(msg); } }; }.start(); }
//弹出更新对话框
/** * 弹出升级对话框 * */ protected void showUpdateDialog() { AlertDialog.Builder builder = new Builder(this); builder.setTitle("有新版本!!"); // builder.setCancelable(false); builder.setMessage(description); builder.setNegativeButton("下次再说", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); gotoHome(); } }); builder.setPositiveButton("立刻升级", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 进行升级 if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { // 下载软件并安装 FinalHttp finalHttp = new FinalHttp(); finalHttp.download(apkurl, Environment .getExternalStorageDirectory().getAbsolutePath() + "/mobiesafe2.0.apk", new AjaxCallBack<File>() { @Override public void onFailure(Throwable t, int errorNo, String strMsg) { t.printStackTrace(); Toast.makeText(getApplicationContext(), "下载失败了", 1).show(); super.onFailure(t, errorNo, strMsg); } @Override public void onLoading(long count, long current) { int progress = (int) (current/count*100); tv_update_info.setText("下载进度: "+progress+"%"); super.onLoading(count, current); } @Override public void onSuccess(File t) { Toast.makeText(getApplicationContext(), "成功", 0).show(); installApk(t); super.onSuccess(t); } private void installApk(File t) { Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.setDataAndType(Uri.fromFile(t), "application/vnd.android.package-archive"); startActivity(intent); } }); } else { Toast.makeText(getApplicationContext(), "sd卡不存在", 0).show(); gotoHome(); } } }); builder.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { gotoHome(); dialog.dismiss(); } }); builder.show(); }
弹出框 使用
AlertDialog.Builder // 其中 要使其点击返回按钮 跳转 要 监听 他的 <span style="font-family: Arial, Helvetica, sans-serif;">setOnCancelListener事件 </span>
<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
//下载完成后 安装apk 其实 就是启动一个Intent 卸载也是一样
//Intent如下
private void installApk(File t) { <span style="white-space:pre"> </span>Intent intent = new Intent(); <span style="white-space:pre"> </span>intent.setAction("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); <span style="white-space:pre"> </span>intent.setDataAndType(Uri.fromFile(t),
"application/vnd.android.package-archive"); <span style="white-space:pre"> </span>startActivity(intent); }