android app版本自动更新,xml解析版本对比

xiaoxiao2021-02-28  11

后台给到一个web xml给我对比版本

new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; try { URL url = new URL("http://android/nyVersion.xml"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); //获取结果码 int code = connection.getResponseCode(); if (code == 200) { InputStream is = connection.getInputStream(); XmlPullParser xmlPullParser = Xml.newPullParser(); xmlPullParser.setInput(is, "UTF-8"); //获取解析的标签的类型 int type = xmlPullParser.getEventType(); while (type != XmlPullParser.END_DOCUMENT) { //循环获取标签 switch (type) { case XmlPullParser.START_TAG: //获取开始标签名字 String starttagName = xmlPullParser.getName(); if ("version".equals(starttagName)) { //获取版本 String version = xmlPullParser.nextText(); int nowVersion = Integer.parseInt(version,10); Log.e("TAG", "run: version "+nowVersion ); if ( nowVersion> oldVersion){ Message message = new Message(); message.what=1;//通知 handler 干活弹出更新窗口 uphandler.sendMessage(message); } } break; case XmlPullParser.END_TAG: break; } //细节: type = xmlPullParser.next(); } } } catch (XmlPullParserException e1) { e1.printStackTrace(); } catch (ProtocolException e1) { e1.printStackTrace(); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } }).start();

从头开始解析xml 发现现在的版本比较低就更新

/** * 提示版本更新的对话框 */ private void showDialogUpdate() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("版本升级"). setIcon(R.drawable.upgrade). setMessage("发现新版本!请及时更新"). setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); Uri content_url = Uri.parse("http://android/app.apk"); intent.setData(content_url); startActivity(intent); } }). setNegativeButton("取消", null); AlertDialog alertDialog = builder.create(); alertDialog.show(); } /* * 获取当前程序的版本号 */ private int getVersionCode() { //获取packagemanager的实例 PackageManager packageManager = getPackageManager(); //getPackageName()是你当前类的包名,0代表是获取版本信息 PackageInfo packInfo = null; try { packInfo = packageManager.getPackageInfo(getPackageName(), 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } Log.e("TAG", "版本号" + packInfo.versionCode); Log.e("TAG", "版本名" + packInfo.versionName); return packInfo.versionCode; }
转载请注明原文地址: https://www.6miu.com/read-2300138.html

最新回复(0)