Android版本更新

来源:互联网 发布:python股票自动化交易 编辑:程序博客网 时间:2024/06/05 20:27

本Demo只是以测试为目的,所以将要对比的服务器的版本号以及要安装的APK放在本地tomcat中

效果图如下:


1.使用pagerManager(包管理者)找出本地版本号

    /**     * 使用packageManager获取本地版本号     */    private float getLocalVersionCode() {        PackageManager pm = getPackageManager();        try {            PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), 0);            return packageInfo.versionCode;        } catch (PackageManager.NameNotFoundException e) {            e.printStackTrace();        }        return -1;//如果没有返回值就返回-1    }

2.使用xutils获取服务器上的版本号

HttpUtils httpUtils = new HttpUtils(3000);        httpUtils.send(HttpRequest.HttpMethod.GET, Constant.URL, new RequestCallBack<String>() {            @Override            public void onSuccess(ResponseInfo<String> responseInfo) {                String result = responseInfo.result;                compareVersion(result);            }            @Override            public void onFailure(HttpException error, String msg) {                Toast.makeText(SplashActivity.this, "失败", Toast.LENGTH_SHORT).show();            }        });


3.将本地版本号与服务器上版本号进行对比,如果本地版本号小于服务器上版本号,弹出对话框进行版本升级

if (localVersionCode < (Float.parseFloat(versionCode))) {//需要进行版本更新            new AlertDialog.Builder(SplashActivity.this)                    .setMessage("是否需要进行版本更新")                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialogInterface, int i) {//                            finish();                            downLoadApk();                        }                    })                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialogInterface, int i) {                            enterMain();                        }                    })                    .show();        } else {//不需要版本更新,直接进入主界面            enterMain();        }    }


4.点击“确定"后,下载服务器上的版本

     /**     * 下载apk     */    private void downLoadApk() {        //先检测sd卡是否存在        if (Environment.getExternalStorageState().equals(Environment.MEDIA_UNMOUNTED)) {            Toast.makeText(SplashActivity.this, "SD卡不存在", Toast.LENGTH_SHORT).show();            return;        }        //进度条显示        final ProgressDialog dialog = new ProgressDialog(this);        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);        dialog.show();        //下载        HttpUtils httpUtils = new HttpUtils(3000);        // url:下载地址        // target:下载之后存在哪里        // callback:下载完之后回掉接口        String target = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + System.currentTimeMillis()                + ".apk";        httpUtils.download(Constant.DOWNLOAD_URL, target, new RequestCallBack<File>() {            @Override            public void onSuccess(ResponseInfo<File> responseInfo) {                Toast.makeText(SplashActivity.this, "下载成功", Toast.LENGTH_SHORT).show();                dialog.dismiss();                //下载成功后调用系统命令进行安装                installApk(responseInfo);            }            @Override            public void onFailure(HttpException error, String msg) {                Toast.makeText(SplashActivity.this, "下载失败", Toast.LENGTH_SHORT).show();                dialog.dismiss();            }            @Override            public void onLoading(long total, long current, boolean isUploading) {                super.onLoading(total, current, isUploading);                dialog.setMax((int) total);                dialog.setProgress((int) current);            }        });    }

5.下载完成后调用系统隐式意图进行安装

/**     * 调用隐式意图进行安装     */    private void installApk(ResponseInfo<File> responseInfo) {        Intent intent = new Intent();        intent.setAction("android.intent.action.VIEW");        intent.addCategory("android.intent.category.DEFAULT");        // Uri data: Uri.fromFile(file)        // String type:mimeType:application/vnd.android.package-archive        intent.setDataAndType(Uri.fromFile(responseInfo.result), "application/vnd.android.package-archive");        startActivityForResult(intent, REQ_INSTALL);    }


Demo下载地址:http://download.csdn.net/detail/k2514091675/9919239

以上步骤就完成了一次版本更新,如有不足之处,欢迎留言讨论


原创粉丝点击