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

后台给到一个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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio 中实现应用程序版本更新,一般可以通过以下步骤进行: 1. 在应用程序的后端服务器上创建一个文件,用于存放最新版本的 apk 文件的下载链接以及版本号等信息。 2. 在应用程序中添加一个检查更新的功能,当用户打开应用程序时,通过与后端服务器交互,检查当前版本是否为最新版本。 3. 如果不是最新版本,则提示用户更新。此时可以采用两种方式: ① 弹出对话框提示用户更新,如果用户点击“确定”按钮,则跳转到下载最新版 apk 文件的页面,让用户下载安装最新版的应用程序。 ② 直接下载最新版 apk 文件,并提示用户安装。此方法需要先在 AndroidManifest.xml 文件中添加下载权限。 以下是一些参考代码: ```java // 检查应用程序是否有新版本 private void checkUpdate() { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url("http://your-backend-server.com/version.json").build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { // 请求失败 } @Override public void onResponse(Call call, Response response) throws IOException { String json = response.body().string(); // 解析 json 文件,获取最新版本号以及 apk 文件下载链接等信息 if (needUpdate) { // 提示用户更新 runOnUiThread(new Runnable() { @Override public void run() { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("发现新版本"); builder.setMessage("是否立即更新?"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 跳转到下载最新版 apk 文件的页面 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(apkUrl)); startActivity(intent); } }); builder.setNegativeButton("取消", null); builder.show(); } }); } } }); } ``` 如果选择直接下载最新版 apk 文件的方式,可以参考以下代码: ```java private void downloadApk() { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(apkUrl).build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { // 下载失败 } @Override public void onResponse(Call call, Response response) throws IOException { InputStream inputStream = response.body().byteStream(); FileOutputStream fos = null; try { File file = new File(getExternalFilesDir(null), "app.apk"); fos = new FileOutputStream(file); byte[] buffer = new byte[2048]; int len; while ((len = inputStream.read(buffer)) != -1) { fos.write(buffer, 0, len); } fos.flush(); // 安装应用程序 installApk(file); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { inputStream.close(); } if (fos != null) { fos.close(); } } } }); } private void installApk(File file) { Intent intent = new Intent(Intent.ACTION_VIEW); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Uri uriForFile = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", file); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setDataAndType(uriForFile, "application/vnd.android.package-archive"); } else { intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); } intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } ``` 以上代码仅供参考,具体实现方式还需根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值