app 版本更新

版本更新:
app 版本更新

  • 从服务器获取更新信息: 1.软件版本号 2. 升级信息 3. 软件下载的url

         private String version;
         private String description;
         private String downloadUrl;
         private String localUrl;
    
  • 根据获取的信息比对软件版本号

    //从服务器获取更新信息
    URL url = new URL(Path.UPDATESERVERURL);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    int code = conn.getResponseCode();
    if (code == 200) {
        InputStream is = conn.getInputStream();
        int len = 0;
        byte[] buf = new byte[512];
        StringBuffer sb = new StringBuffer();
        while ((len = is.read(buf)) != -1) {
        sb.append(new String(buf, 0, len, "utf-8"));
        }
        is.close();
        conn.disconnect();
        String jsonStr = sb.toString();
        sb.setLength(0);
        sb = null;
        LogUtil.i(jsonStr);
        Gson gson = new Gson();
        mUpdateInfo = gson.fromJson(jsonStr, UpdateInfo.class); //解析json字符串
    }
    

    //比对信息

    String version = getVersoin();
    version = version.replace(".", "");
    int versionCode = Integer.parseInt(version);
    if (mUpdateInfo != null) {
        if (versionCode < Integer.parseInt(mUpdateInfo.getVersion().replace(".", ""))) {
            AlertDialog.Builder builder = new Builder(getActivity());
            builder.setCancelable(false);
            builder.setTitle("升级提醒");
            builder.setMessage("版本号:" + mUpdateInfo.getVersion() + "\n" + "更新详情:" + mUpdateInfo.getDescription());
            builder.setNegativeButton("取消", null);
            builder.setPositiveButton("更新", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    LogUtil.i("确定更新");
                    String state = Environment.getExternalStorageState();
                    LogUtil.i("state: " + state);
                    if (Environment.MEDIA_MOUNTED.equals(state)) {
                    // 下载文件
                    downloadApk();
                    } else {
                    MyToast.showMsg(getActivity(), "存储设备异常,更新失败");
                    }
                }
            });
            builder.show();
        }
    }
    

    //获取软件版本号

    private String getVersoin() {
        try {
            PackageInfo pi = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0);
            return pi.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            LogUtil.i("获取版本号出错");
            return null;
        }
    }
    
  • 根据软件版本号对比,根据下载地址下载

    protected void downloadApk() {
        final ProgressDialog pd = new ProgressDialog(getActivity());
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setTitle("下载");
        pd.setMessage("正在下载,请稍候...");
        pd.show();
        pd.setCancelable(false);
        pd.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (TextUtils.isEmpty(mUpdateInfo.getLocalUrl())) {
                    MyToast.showMsg(getActivity(), "下载出错了");
                } else {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(new File(mUpdateInfo.getLocalUrl())),
                    "application/vnd.android.package-archive");
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
        new Thread() {
            public void run() {
                try {
                    String apkUrl = mUpdateInfo.getDownloadUrl();
                    URL url = new URL(apkUrl);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.connect();
                    int code = conn.getResponseCode();
                    if (code == 200) {
                        int length = conn.getContentLength();
                        pd.setMax(length);
                        String fileName = apkUrl.substring(apkUrl.lastIndexOf("/"));
                        File file = new File(Environment.getExternalStorageDirectory(),
                        getActivity().getPackageName() + fileName);
                        if (!file.exists()) {
                            file.getParentFile().mkdir();
                        }
                        mUpdateInfo.setLocalUrl(file.getAbsolutePath());
                        FileOutputStream fos = new FileOutputStream(file);
                        InputStream is = conn.getInputStream();
                        int len = 0;
                        int progress = 0;
                        byte[] buf = new byte[1024];
                        while ((len = is.read(buf)) != -1) {
                            fos.write(buf, 0, len);
                            progress += len;
                            pd.setProgress(progress);
                        }
                        fos.flush();
                        fos.close();
                        is.close();
                        conn.disconnect();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    mUpdateInfo.setLocalUrl("");
                } finally {
                    pd.dismiss();
                }
            }
        }.start();
    }
    
  • 下载完成安装

    pd.setOnDismissListener(new OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            if (TextUtils.isEmpty(mUpdateInfo.getLocalUrl())) {
                MyToast.showMsg(getActivity(), "下载出错了");
            } else {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(new File(mUpdateInfo.getLocalUrl())),
                "application/vnd.android.package-archive");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        }
    });
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在UniApp中,实现app版本更新可以通过以下步骤进行操作。首先,在关于我们的页面中应添加版本更新操作,可以通过后端加入一个字段来标识是否需要进行强制更新。如果需要强制更新,那么在进入app时就需要进行版本检测和对比,如果当前版本与最新版本不一致,则必须进行更新,否则无法使用该app。如果版本一致,则无需进行提示。这个逻辑基本与非强制更新相同。 在打包时,还需要注意两个重要的修改。首先,要修改应用版本名称,确保其在升级时高于上一次设置的版本号。其次,要修改应用版本号,确保其为一个整数,并且在升级时高于上一次设置的值,这样在更新app时需要下载最新的包才不会出现问题。 另外,UniApp还提供了一个版本更新的js文件,该文件省去了自己编写布局和进度条的麻烦。通过使用这个js文件,可以直接生成更新弹窗,并且可以配置是否进行强制更新,并且还可以看到更新的进度。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [uniapp版本更新](https://blog.csdn.net/m0_51431448/article/details/130326109)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [uni-app版本更新](https://download.csdn.net/download/weixin_44052462/13188942)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值