Android13 下载apk并安装apk

下载代码

java 复制代码
private void handleUpdate(String code, String file_path) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // 提示用户有新版本
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle("发现新版本")
                    .setMessage("新版本:是否立即下载?")
                    .setPositiveButton("下载", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    downloadFile(file_path);
                                }
                            }).start();
                        }
                    })
                    .setNegativeButton("取消", (new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    }))
                    .show();
        }
    });

 }

downloadFile代码

需要下载okhttp

implementation 'com.squareup.okhttp3:okhttp:4.10.0'

java 复制代码
private void downloadFile(String URL) {
    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url(URL)
            .build();

    try (Response response = client.newCall(request).execute()) {
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        // Write theResponseBody in entirety to the destination.
        BufferedSink sink = Okio.buffer(Okio.sink(new File(getFilesDir().getAbsolutePath() + "/apk.apk")));
        sink.writeAll(response.body().source());
        sink.close();
        installApk(getFilesDir().getAbsolutePath() + "/apk.apk");
    } catch (Exception e) {
    }

}

安装APK installApk

java 复制代码
private void installApk(String filePath) {
    File file = new File(filePath);
    Uri contentUri = FileProvider.getUriForFile(
            this,
            BuildConfig.APPLICATION_ID + ".fileprovider",
            file);

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(intent);
}

在AndroidManifest.xml里添加

java 复制代码
<provider
   android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

在xml里新建file_paths.xml文件

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path
        name="my_files_dir"
        path="/" />
</paths>
相关推荐
夜晚回家28 分钟前
「Java基本语法」代码格式与注释规范
java·开发语言
斯普信云原生组39 分钟前
Docker构建自定义的镜像
java·spring cloud·docker
wangjinjin18042 分钟前
使用 IntelliJ IDEA 安装通义灵码(TONGYI Lingma)插件,进行后端 Java Spring Boot 项目的用户用例生成及常见问题处理
java·spring boot·intellij-idea
wtg445243 分钟前
使用 Rest-Assured 和 TestNG 进行购物车功能的 API 自动化测试
java
白宇横流学长1 小时前
基于SpringBoot实现的大创管理系统设计与实现【源码+文档】
java·spring boot·后端
fat house cat_2 小时前
【redis】线程IO模型
java·redis
stein_java3 小时前
springMVC-10验证及国际化
java·spring
weixin_478689763 小时前
C++ 对 C 的兼容性
java·c语言·c++
LUCIAZZZ3 小时前
HikariCP数据库连接池原理解析
java·jvm·数据库·spring·springboot·线程池·连接池
sky_ph3 小时前
JAVA-GC浅析(二)G1(Garbage First)回收器
java·后端