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>
相关推荐
知识分享小能手13 分钟前
Ubuntu入门学习教程,从入门到精通,Ubuntu 22.04中的Java与Android开发环境 (20)
java·学习·ubuntu
南屿欣风20 分钟前
FeignClient 踩坑:@FeignClient 同时配 value 和 url 的 “无效服务名” 问题
java
豆沙沙包?39 分钟前
2026年--Lc329-735. 小行星碰撞(栈)--java版
java·开发语言
PuddingSama43 分钟前
Gson 很好,但在Kotlin上有更合适的序列化工具「Kotlin Serialization」
android·kotlin·gson
爆更小哇1 小时前
Selenium自动化测试函数全解析(二)
java·selenium·测试工具·自动化
C雨后彩虹1 小时前
计算误码率
java·数据结构·算法·华为·面试
fanruitian1 小时前
Springboot项目父子工程
java·数据库·spring boot
小肖爱笑不爱笑1 小时前
SpringBoot Web
java·http协议·分层解耦·web后端
柒.梧.1 小时前
Spring核心知识全解析:从入门实战到进阶
java·后端·spring
乌日尼乐1 小时前
【Java基础整理】Java字符串处理,String、StringBuffer、StringBuilder
java·后端