安卓版本升级,更新版本开发!

流程图

在这里插入图片描述

准备

安装okhttp,为了从远程下载apk

implementation(“com.squareup.okhttp3:okhttp:4.9.1”)

在这里插入图片描述
AndroidManifest.xml中,配置权限

    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /><!-- 用于安装APK -->
    <uses-permission android:name="android.permission.WAKE_LOCK" /> <!-- 用于进行网络定位 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 用于申请调用A-GPS模块 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- 用于申请调用A-GPS模块 -->
    <uses-permission android:name="android.permission.INTERNET"/><!--网络权限声明-->
	<!--com.example.mytest3  是自己的包名-->
	<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.example.mytest3.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <paths >
        <!--files-path  相当于 getFilesDir()-->
        <files-path
            name="my_images"
            path="." />
        <!--cache-path  相当于 getCacheDir()-->
        <cache-path
            name="cache"
            path="." />
        <!--external-path  相当于 Environment.getExternalStorageDirectory()-->
        <external-path
            name="camera"
            path="." />
        <!--external-files-path  相当于 getExternalFilesDir("") -->
        <external-files-path
            name="exfilesdir"
            path="." />
        <!--external-cache-path  相当于 getExternalCacheDir() -->
        <external-cache-path
            name="excachedir"
            path="." />
        <!-- /storage/emulated/0/Download/com.bugly.upgrade.demo/.beta/apk-->
        <external-path
            name="beta_external_path"
            path="Download/" />
        <!--/storage/emulated/0/Android/data/com.bugly.upgrade.demo/files/apk/-->
        <external-path
            name="beta_external_files_path"
            path="Android/data/" />
    </paths>
</resources>

开始写方法

编写一个弹框

        Integer loc = 11; // 本地服务器版本,
        Integer myServer = 12; // 远程服务器版本
        Boolean mustUpdate = false; // 是否强制更新
        String url ="https://aaa.oss-cn-chengdu.aliyuncs.com/app-release.apk"; // 模拟一个服务器apk
        // 这里是为了方便就这么写固定值,正常情况需要调接口访问
        if(myServer>loc) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("版本升级")
                    .setIcon(R.mipmap.a)
                    .setPositiveButton("确定",new DialogInterface.OnClickListener(){

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            try {
                                downLoad1(url);
                            } catch (MalformedURLException e) {
                                e.printStackTrace();
                            };
                        }
                    })
                    .setNegativeButton("取消",null)
                    .setMessage("发现新版本,需要升级么")
                    .create()
                    .show();
        }

下载方法

    private void downLoad1(String url) throws MalformedURLException {
        Request request = new Request.Builder().url(url).build();
        OkHttpClient client = new OkHttpClient();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onResponse(@NonNull okhttp3.Call call, @NonNull Response response) throws IOException {
                if (response.isSuccessful()) {
                    saveApk(response.body().byteStream());
                }
            }

            @Override
            public void onFailure(@NonNull okhttp3.Call call, @NonNull IOException e) {
                e.printStackTrace();
            }

        });



    }

保存下载之后的apk存起来

 private void saveApk(InputStream inputStream) {
        File file = new File(this.getExternalFilesDir(null), "app-release.apk");
        try (FileOutputStream outputStream = new FileOutputStream(file)) {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, length);
            }
            installApk(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

安装

private void installApk(File apkFile) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri apkUri = FileProvider.getUriForFile(this, this.getPackageName() + ".provider", apkFile);
        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        this.startActivity(intent);
    }

启动程序就可以看到效果了。
温馨提升:
必须是打过的包,不能用开发的时候发文,会报签名问题。
开发调试的时候,不会使用前面的!!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值