获取版本号
/** * 获取版本号 * * @return */ private String getVersionName() { PackageManager pm = getPackageManager();// 用来管理手机的apk try { PackageInfo info = pm.getPackageInfo(getPackageName(), 0);// 得到指定apk的功能清单 return info.versionName; } catch (NameNotFoundException e) { e.printStackTrace(); } return ""; }
检查更新
private void checkUpadate() { new Thread() { @Override public void run() { long startTime = System.currentTimeMillis(); Message mes = Message.obtain(); try { URL url = new URL(getString(R.string.versionURL)); // 联网 HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.setRequestMethod("GET");// 要 大写 conn.setConnectTimeout(4000); int code = conn.getResponseCode(); Log.i("select", code + ""); if (code == 200) { // 联网成功 InputStream is = conn.getInputStream(); // 把流转为String 类型 String result = StreamTools.readFromStream(is); Log.i("SplashActivity", result); // json 解析 JSONObject obj = new JSONObject(result); String version = (String) obj.get("version"); desciption = (String) obj.get("description"); apkurl = (String) obj.get("apkurl"); if (getVersionName().equals(version)) { // 版本一致, 没有新版本, 进入主页面 mes.what = ENTER_HOME; } else { // 有新版本 ,弹出一堆话框 mes.what = SHOW_UPDATE_DIALOG; } } } catch (MalformedURLException e) { e.printStackTrace(); mes.what = URL_ERROR; } catch (IOException e) { e.printStackTrace(); mes.what = NET_ERROR; } catch (JSONException e) { e.printStackTrace(); mes.what = JSON_ERROR; } finally { long endTime = System.currentTimeMillis(); // 花了 多少时间 long dTime = endTime - startTime; if (dTime < 2000) { try { Thread.sleep(2000 - dTime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } handler.sendMessage(mes); } } }.start(); }
handle 弹窗 判断是否升级
private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case SHOW_UPDATE_DIALOG: Log.i("select", "显示升级的对话狂"); showUpdateDialog(); break; case ENTER_HOME: enterHome(); break; case URL_ERROR: enterHome(); Toast.makeText(getApplicationContext(), "URL_ERROR错误", 1) .show(); break; case NET_ERROR: enterHome(); Toast.makeText(getApplicationContext(), "NET_ERROR错误", 1) .show(); break; case JSON_ERROR: enterHome(); Toast.makeText(getApplicationContext(), "JSON_ERROR错误", 1) .show(); break; } } /** * 弹出升级对话框 */ private void showUpdateDialog() { // http://zhidao.baidu.com/link?url=N7UoPhqAMbgFbrWkChLQVpO_7Rvae6ywzMwTIwgEmznqeiYsDpdF2qLcSHmItgPID500zdsATaQAPPCLLPQs7q AlertDialog.Builder builder = new Builder(SplashActivity.this); builder.setTitle("提示升级"); // builder.setCancelable(false);//强制升级 builder.setMessage(desciption); builder.setOnCancelListener(new OnCancelListener() {// 随便点 或者点返回 @Override public void onCancel(DialogInterface dialog) { // 默认取消安装 进入主页面 enterHome(); dialog.dismiss(); } }); builder.setNegativeButton("下次再说", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); enterHome(); } }); builder.setPositiveButton("立刻升级1", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 下载apk 替换安装 if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { // SDCARD 存在 // afnal FinalHttp finalHttp = new FinalHttp(); finalHttp.download(apkurl, Environment .getExternalStorageDirectory() .getAbsolutePath() + "/mobilesave2.0apk", 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) { super.onLoading(count, current); int progress = (int) (current * 100 / count); tv_update_info .setVisibility(View.VISIBLE); tv_update_info.setText("下载进度:" + progress + "%"); } @Override public void onSuccess(File t) { super.onSuccess(t); installAPK(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(), "没有sdcard", 0) .show(); return; } } }); builder.show(); } };