当前位置: 首页 > article >正文

Android13 下载apk并安装apk

下载代码

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’

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

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里添加

<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文件

<?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>

http://www.kler.cn/news/307872.html

相关文章:

  • 跳出大厂圈子——普通程序员如何开启逆袭之路
  • mac上什么压缩软件没有广告,苹果电脑解压软件BetterZip有广告吗
  • 深入理解MATLAB中的事件处理机制
  • ROS组合导航笔记:融合传感器数据
  • 嵌入式数据库sqlite和rocksdb的介绍以及对比
  • 分治算法归并排序
  • CSP-J/S赛前知识点大全2:初赛纯靠记忆的知识点
  • Docker高级管理之compose容器编排与私有仓库的部署
  • FPGA实现串口升级及MultiBoot(四)MultiBoot简介
  • [苍穹外卖]-12Apache POI入门与实战
  • 滚雪球学SpringCloud[2.1]:服务注册中心Eureka
  • robomimic基础教程(三)——自带算法
  • 【Linux】ICMP
  • 【开端】docker基线漏洞修复
  • React-Hooks-Form 集成 Zod 校验库
  • go get -u @latest没有更新依赖模块
  • 如何通过深度学习实践来理解深度学习的核心概念
  • Ubuntu 24.04中安装virtualenv
  • QT + WebAssembly + Vue环境搭建
  • JS面试真题 part4
  • 【Spring框架精讲】进阶指南:企业级Java应用的核心框架(Spring5)
  • NX二次开发—批量导出点工具
  • html限制仅有一个音/视频可播放
  • 阿里云社区领积分自动打卡Selenium IDE脚本
  • How to see if openAI (node js) createModeration response “flagged“ is true
  • 代码随想录算法训练营第五十八天 | 拓扑排序精讲-软件构建
  • Arduino IDE离线配置第三方库文件-ESP32开发板
  • 8.JMeter+Ant(基于工具的实现接口自动化,命令行方式)
  • 常见的限流算法
  • 【C/C++】程序的构建(编译)过程概述