Android 本地更新APK(无需添加运行时权限)

2023-11-19

很多APP都会有自动更新APP然后本地安装的功能 之前一直是用AsnycTask来做的 最近发现AsyncTask被标记为过时 那么就换一种方式来写吧
我自己是做在Dialog里面 使用okhttp进行文件下载 配合自定义View的进度条进行展示的
首先老规矩上图
开始下载了
下载完成后自动进入安装页面

话不多说 我们先来定义一个用来回调的接口 分别对应成功 失败 和进度

interface DownloadListener {
    fun onSuccess()
    fun onFailed(msg: String)
    fun onProgress(progress: Int)
}

首先就是我们对于文件的处理

//首先是我们的下载地址 没什么好说的就使用app本地的file文件夹就可以了
 val path = getExternalFilesDir(null)!!.absolutePath+"/app.apk"
//先生成我们的文件
 val file = File(path)
//这里看你需求 是否需要短线点续传  需要的话就记录一下当前下载长度
 var currentLength = 0L
  if (file.exists()){
//  currentLength=file.length()//下载长度 如果你需要断点续传就使用这个参数并去掉删除文件的方法
       file.delete()
   }

接着创建一个okhttp实例并获取我们目标文件的总长度(大小)用来处理我们下载的百分比

    private val client = OkHttpClient()
    val request = Request.Builder().url(url).build()
        val response = client.newCall(request).execute()
        return if (response.isSuccessful) {
            //获取文件长度
            val length = response.body?.contentLength() as Long
            response.body!!.close()
            length
        } else {
          //如果回调失败了就返回长度为0
            0
        }

根据长度来判断文件是否下载完了

 val fileLength = fileLength(url)//获取文件总长度
     if (fileLength == 0L) {//如果获取的文件长度为0则失败
         listener.onFailed("文件长度为0")
    } else if (currentLength == fileLength) {//如果获取的文件长度和下载的文件长度相等则下载完毕
           listener.onSuccess()
    }

接着就要开始下载文件咯

  //使用OkHttp进行APK下载
            //请求头为RANGE 如果是断点续传就是bytes=[start,end] 
            val request = Request.Builder().addHeader("RANGE", "bytes=$currentLength-").url(url).build()
            val response = client.newCall(request).execute()
            val inputStream = response.body?.byteStream()
            val saverFile = RandomAccessFile(file, "rw")
            saverFile.seek(currentLength)
            val b = ByteArray(1024)
            var total = 0
            var len = inputStream!!.read(b)
            while (len != -1) {
                total += len
                saverFile.write(b, 0, len)
                val progress = ((total + currentLength) * 100 / fileLength).toInt()
                //下载的百分比回调
                listener.onProgress(progress)
                len = inputStream.read(b)
            }
            response.body?.close()
            inputStream.close()
            saverFile.close()
            //下载完毕
            listener.onSuccess()

文件下载完毕就可以进行apk的安装了

val file=File(path)
        val intent = Intent(Intent.ACTION_VIEW)
        intent.setDataAndType(Uri.parse("file://$file"), "application/vnd.android.package-archive")
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //这边使用你自己定义的FileProvider 此处就不进行讲解了
            val contentUri = FileProvider.getUriForFile(this, "com.xxxx.provider", file)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive")
        } else {
            intent.setDataAndType(Uri.parse("file://$file"), "application/vnd.android.package-archive")
        }
        startActivity(intent)
        //杀掉APP
        android.os.Process.killProcess(android.os.Process.myPid())

另外需要使用的权限有 都不是运行时权限

//安装本地APK的权限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
//访问网络的权限
 <uses-permission android:name="android.permission.INTERNET" />

好了完结散花 最后送上我封装好的代码
首先是接口

interface DownloadListener {
    fun onSuccess()
    fun onFailed(msg: String)
    fun onProgress(progress: Int)
}

//然后是下载的工具类

object DownloadUtil{

    private val client = OkHttpClient()

    fun download(url:String,path:String,listener:DownloadListener){
        thread {
            val file = File(path)
            var currentLength = 0L
            if (file.exists()){
//                currentLength=file.length()//下载长度 如果你需要断点续传就使用这个参数并去掉删除的方法
                file.delete()
            }
            val fileLength = fileLength(url)//获取文件总长度
            if (fileLength == 0L) {//如果获取的文件长度为0则失败
                listener.onFailed("文件长度为0")
            } else if (currentLength == fileLength) {//如果获取的文件长度和下载的文件长度相等则下载完毕
                listener.onSuccess()
            }
            //使用OkHttp进行APK下载
            //请求头为RANGE 如果是断点续传就是bytes=[start,end]
            val request = Request.Builder().addHeader("RANGE", "bytes=$currentLength-").url(url).build()
            val response = client.newCall(request).execute()
            val inputStream = response.body?.byteStream()
            val saverFile = RandomAccessFile(file, "rw")
            saverFile.seek(currentLength)
            val b = ByteArray(1024)
            var total = 0
            var len = inputStream!!.read(b)
            while (len != -1) {
                total += len
                saverFile.write(b, 0, len)
                val progress = ((total + currentLength) * 100 / fileLength).toInt()
                //下载的百分比回调
                listener.onProgress(progress)
                len = inputStream.read(b)
            }
            response.body?.close()
            inputStream.close()
            saverFile.close()
            //下载完毕
            listener.onSuccess()
        }
    }

    //获取文件长度
    private fun fileLength(url: String): Long {
        val request = Request.Builder().url(url).build()
        val response = client.newCall(request).execute()
        return if (response.isSuccessful) {
            val length = response.body?.contentLength() as Long
            response.body!!.close()
            length
        } else {
            0
        }
    }
}

接着是用来展示下载页面的Dialog

  class UpdateDialog( private val downloadUrl: String, private val path: String, private val because: String) : DialogFragment() {
    //进度展示
    private var mProgressTV: TextView? = null
    private var mBecauseTV: TextView? = null
    private var mUpdateViw: UpdateProgressView? = null
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view=inflater.inflate(R.layout.layout_update_dialog,container,false)
        mProgressTV = view.findViewById(R.id.mProgressTV)
        mBecauseTV = view.findViewById(R.id.mBecauseTV)
        mUpdateViw = view.findViewById(R.id.mUpdateViw)
        mBecauseTV?.text = because.handlerNull()

        DownloadUtil.download(downloadUrl, path, object : DownloadListener {
            override fun onSuccess() {
                updateApp(path)
            }

            override fun onFailed(msg: String) {
                myLog("下载失败了")
            }

            override fun onProgress(progress: Int) {
                myLog("下载百分比:$progress %")
                mUpdateViw?.handlerPercent(progress)
                requireActivity().runOnUiThread {
                    val  p="正在升级中${progress}%"
                    mProgressTV?.text = p
                }
            }
        })
        return view
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.CommonDialog)
    }


    private fun updateApp(path: String) {
        MyLog.d("安装APP")
        val file = File(path)
        val intent = Intent(Intent.ACTION_VIEW)
        intent.setDataAndType(Uri.parse("file://$file"), "application/vnd.android.package-archive")
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            val contentUri = FileProvider.getUriForFile(requireContext(), "com.xxxx.provider", file)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive")
        } else {
            intent.setDataAndType(Uri.parse("file://$file"), "application/vnd.android.package-archive")
        }
        requireActivity().startActivity(intent)
        //杀掉APP
        android.os.Process.killProcess(android.os.Process.myPid())
    }
}

dialog的页面 这里使用的展示进度条的自定义View可以看我另外一篇文章
https://www.jianshu.com/p/001fc038b557

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:gravity="center"
	android:orientation="horizontal">

	<LinearLayout
		android:gravity="center_horizontal"
		android:layout_width="260dp"
		android:layout_height="300dp"
		android:background="@drawable/bg_solid_whit_radius5dp"
		android:orientation="vertical"
		tools:ignore="UselessParent">
		<TextView
			android:id="@+id/mBecauseTV"
			android:textSize="12sp"
			android:layout_marginTop="20dp"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"/>
		<com.android.bowenporjectv2.weiget.UpdateProgressView
			android:id="@+id/mUpdateViw"
			android:layout_width="150dp"
			android:layout_height="150dp"/>

		<TextView
			android:layout_marginHorizontal="20dp"
			android:id="@+id/mProgressTV"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_gravity="center_horizontal"
			android:layout_marginTop="20dp"
			android:text="正在升级中...%"
			android:textSize="12sp" />
	</LinearLayout>
</LinearLayout>

dialog的样式

 <style name="CommonDialog">
        <item name="android:windowCloseOnTouchOutside">true</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

最后就是使用了

//下载地址
            val url = "你自己app的下载地址"
            //对应手机android/data/你的包名下/files/
            val path = getExternalFilesDir(null)!!.absolutePath+"/app.apk"
            UpdateDialog(url,path,"优化app").show(supportFragmentManager,"")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 本地更新APK(无需添加运行时权限) 的相关文章

随机推荐

  • STM32移植freemodbusRTU(hal库)从机

    MODBUS源码下载 freemodbus源码 github地址 一 移植准备 1 cubemx创建基础工程 配置串口和定时器以及时钟 2 拷贝freertos源码到工程目录 新建一个freemodbus文件夹 拷贝modbus文件夹 3
  • 编码 & 8421BCD 码的故事

    计算机编码中 我们都是先了解了二进制 其中分有符号数 无符号数 然后会接触到BCD码 那么BCD码是怎么产生的 为什么又要用四位二进制来表示呢 8421BCD 码的故事 一 BCD码 1 由来 2 8421BCD码 3 修正 二 底层验证修
  • 是面试官放水,还是公司实在是太缺人?这都没挂,华为原来这么容易进...

    华为是大企业 是不是很难进去啊 在华为做软件测试 能得到很好的发展吗 一进去就有9 5K 其实也没有想的那么难 直到现在 心情都还是无比激动 本人211非科班 之前在字节和腾讯实习过 这次其实没抱着什么特别大的希望投递 没想到华为可以再给我
  • 编译内核linux-2.6.38 出现error (2013-03-28 10:42)

    内核建议到官网下载 当然如果签名对的话也可以 解压后 保险起见 make mrproper 然后 make oldconfig 最后 make menuconfig 配置内核 然后再开始编译 在编译内核linux 2 6 38 出现以下问题
  • Android 透明状态栏

    转载 https blog csdn net fan7983377 article details 51604657 最近公司产品提出透明状态栏的要求 将一张背景填充满屏幕 自己记录一下 Android 透明状态栏 有两种 背景是图片还是纯
  • PHP 生成excel

    PHP 生成excel 好用强大的php excel类库 做Magento的订单导出Excel功能 找了这个php的excel类 PHPExcel PHPExcel是强大的 MS Office Excel 文档生成类库 基于Microsof
  • 课程笔记3

    一 以太坊 比特币被称为区块链1 0 以太坊被称为区块链2 0 以太坊的符号是ETH 以太币的符号是Ether 单位是Wei 比特币的符号是BTC 单位是Satoshi 以太坊做出的改进 在以太坊中出块时间减少到十几秒 比特币的mining
  • iOS实训笔记—调用系统相机与网络请求

    iOS开发实训第三周周报 总结 本周开始进行项目的开发 目前小组计划共同完成前端开发 我负责的部分为个人页面 其中涉及到加载个人信息时 需要从相册或相机获取图片 作为头像上传 并进行网络请求 获取资源 因此本周周报总结这部分的内容 学习知识
  • NeRF学习笔记(含公式、图解和过程)

    NeRF学习笔记 关注公众号 不定期分享NeRF相关文献 引言 NeRF Representing Scenes as Neural Radiance Fields for View Synthesis作为2020年ECCV的一篇论文 在用
  • 51单片机的智能饮水机控制系统【proteus仿真+程序+原理图】

    1 主要功能 该系统由AT89C51单片机 LCD1602模块 DS18B20温度传感器模块 DS1302时间模块 继电器驱动模块 电位器模块构成 适用于智能饮水机 智能水杯等相似项目 可实现功能 版本一 1 LCD1602实时显示时间 水
  • 在CentOS上安装桌面环境(例如GNOME)

    可以按照以下步骤在 CentOS 上安装桌面环境 例如 GNOME 确保您的 CentOS 系统已连接到互联网 并拥有 root 或具有 sudo 权限的用户 打开终端 并使用 yum 包管理器更新系统 sudo yum update 安装
  • MSP430嵌入式接口编程(惯性测量单元温湿度双音多频磁力计LCD显示等)

    Energia IDE编程MSP430 GPIO 串口通讯 定时中断 添加库 嵌入式器件接口编程 加速度计 include
  • 全 民 进 入 互 联 网

    2015年 3C行业的变化有目共睹 互联网 的概念全面深入人心 贯穿于企业经营和百姓的日常生活中 通讯行业提速降费 诸多国产精品手机现身 电商行业更加规范 移动端超越PC端成为主流渠道 家电行业诞生多个新技术 智能家电格局正在改写 让我们一
  • C++实现FFT频谱分析

    Update 9 10 2022 鸽了太久 增补了一些新的表述和简单推导 以及FFT在算法竞赛中的应用部分 帖子里的代码已经分别在2021全国大学生电子设计竞赛 洛谷OJ和课程设计中实战过 可靠性有保障 Origin 10 23 2021
  • web前端技术笔记(九)JavaScript介绍、变量、操作元素属性

    JavaScript JavaScript介绍 变量 变量类型 变量 函数 属性 函数参数命名规范 获取元素方法一 操作元素属性 通过 操作属性 通过 操作属性 innerHTML JavaScript介绍 JavaScript是运行在浏览
  • Ant-Maven-Gradle

    make Makefile学习 peterYong 博客园 ant ant 工具 milkty 博客园 maven 学习Maven这一篇就够了 轻松的小希的博客 CSDN博客 学Maven 这篇万余字的教程 真的够用了 江南一点雨 博客园
  • CSS 样式书写规范,css样式书写规范

    在工作当中css样式是非常重要的 但是咋样书写css样式更重要 一 css书写规范 1 定位属性 position display float left top right bottom overflow clear z index 2 自
  • 千与千寻 中日歌词与罗马音译(最准确啦)

    千与千寻 国语和日语版 Cover 木村 弓 作曲 木村 弓 作词 觉 和歌子 张 就此告别吧水上的列车就快到站 粥 呼 胸 奥 yo n de i ru mu ne no do ko ka o ku de 张 开往未来的路上没有人会再回返
  • MySQL 触发器入门 (转载)

    博客迁移 时空蚂蚁http cui zhbor com MySQL 5 1包含对触发器的支持 触发器是一种与表操作有关的数据库对象 当触发器所在表上出现指定事件时 将调用该对象 即表的操作事件触发表上的触发器的执行 创建触发器 在MySQL
  • Android 本地更新APK(无需添加运行时权限)

    很多APP都会有自动更新APP然后本地安装的功能 之前一直是用AsnycTask来做的 最近发现AsyncTask被标记为过时 那么就换一种方式来写吧 我自己是做在Dialog里面 使用okhttp进行文件下载 配合自定义View的进度条进