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>
相关推荐
lizi8888812 分钟前
足球大小球及亚盘数据分析与机器学习实战详解:从数据清洗到模型优化
java·人工智能·机器学习·数据挖掘·数据分析
邂逅自己22 分钟前
Java入门程序-HelloWorld
java·开发语言
Good_tea_h36 分钟前
Android中如何处理运行时权限?
android
SkyrimCitadelValinor38 分钟前
Java【泛型】
java
冬田里的一把火338 分钟前
[Android][Reboot/Shutdown] 重启/关机 分析
android·gitee
java_heartLake39 分钟前
设计模式之工厂方法模式
java·设计模式·工厂方法模式
大海..44 分钟前
Android 系统开发人员的权限说明文档
android
自身就是太阳1 小时前
Maven的高级特性
java·开发语言·数据库·后端·spring·maven
飞翔的佩奇1 小时前
Java项目: 基于SpringBoot+mybatis+maven课程答疑系统(含源码+数据库+毕业论文)
java·数据库·spring boot·毕业设计·maven·mybatis·课程答疑