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>
相关推荐
萌新下岸多多关照几秒前
Java中synchronized 关键字
java·开发语言
中国lanwp2 分钟前
使用Maven部署WebLogic应用
java·maven
开开心心就好12 分钟前
Word图片格式调整与转换工具
java·javascript·spring·eclipse·pdf·word·excel
CGG9242 分钟前
【单例模式】
android·java·单例模式
苦学编程的谢44 分钟前
多线程代码案例-1 单例模式
java·开发语言·单例模式
yaoxin5211231 小时前
80. Java 枚举类 - 使用枚举实现单例模式
java·开发语言·单例模式
夏季疯1 小时前
学习笔记:黑马程序员JavaWeb开发教程(2025.4.7)
java·笔记·学习
kp000001 小时前
PHP弱类型安全漏洞解析与防范指南
android·开发语言·安全·web安全·php·漏洞
卡戎-caryon2 小时前
【C++】15.并发支持库
java·linux·开发语言·c++·多线程
头发那是一根不剩了2 小时前
怎么用idea分析hprof文件定位JVM内存问题
java·jvm