Android app在后台静默升级,安装成功之后自动打开app

您所在的位置:网站首页 android9升级 Android app在后台静默升级,安装成功之后自动打开app

Android app在后台静默升级,安装成功之后自动打开app

2024-07-14 22:40:50| 来源: 网络整理| 查看: 265

一、使用Android自带的DownloadManager远程下载APK;

val handler = Handler() private fun downLoadApk(apkUrl: String) { //创建request对象 val request: DownloadManager.Request = DownloadManager.Request(Uri.parse(apkUrl)) //显示还是隐藏 隐藏加权限:DOWNLOAD_WITHOUT_NOTIFICATION //request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN) //设置通知栏的标题 request.setTitle("下载") //设置通知栏的message request.setDescription("正在下载.....") //设置下载文件类型 request.setMimeType("application/vnd.android.package-archive") //设置文件存放目录 request.setDestinationInExternalFilesDir( this, Environment.DIRECTORY_DOWNLOADS, "update.apk" ) //获取系统服务 val downloadManager = getSystemService(DOWNLOAD_SERVICE) as DownloadManager //进行下载 val id = downloadManager.enqueue(request) val runnable = object : Runnable { override fun run() { val cursor = downloadManager.query(DownloadManager.Query().setFilterById(id)) cursor?.apply { if (moveToFirst()) { val title = getString(getColumnIndex(DownloadManager.COLUMN_TITLE)) val uri = getString(getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)) val current = getInt(getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)) val total = getInt(getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)) LogUtil.e(current.toString()) //获取下载状态信息 if (getInt(getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) { //下载完成 handler.removeCallbacksAndMessages(null) LogUtil.e("下载完成:$uri") val file = File(uri.substring(7) RootUtil.sinlenceInstallApk(file) } } } cursor?.close() handler.postDelayed(this, 1000) } } handler.postDelayed(runnable, 1000) }

二,下载完之后,通过封装的RootUtils工具类安装重启

object RootsUtils { /** * 判断手机是否有root权限 */ private fun sysHasRootPerssion(): Boolean { var printWriter: PrintWriter? var process: Process? = null try { process = Runtime.getRuntime().exec("su") printWriter = PrintWriter(process.outputStream) printWriter.flush() printWriter.close() val value = process.waitFor() return returnResult(value) } catch (e: Exception) { e.printStackTrace() } finally { process?.destroy() } return false } /** * root下执行cmd的返回值 */ private fun returnResult(value: Int): Boolean { // 代表成功 return when (value) { 0 -> { true } 1 -> { // 失败 false } else -> { // 未知情况 false } } } /** * root下静默安装 */ private fun rootSlienceInstallApk(apkPath: String): Boolean { var printWriter: PrintWriter? var process: Process? = null try { process = Runtime.getRuntime().exec("su") printWriter = PrintWriter(process.outputStream) printWriter.println("pm install -r $apkPath") printWriter.flush() printWriter.close() execLinuxCommand() val value = process.waitFor() return returnResult(value) } catch (e: Exception) { e.printStackTrace() } finally { process?.destroy() } return false } /** * 通过Linux延时执行重启 */ private fun execLinuxCommand() { val cmd = "sleep 1; am start -n com.xx.xxx/.LunchActivity" //Runtime对象 val runtime = Runtime.getRuntime() try { val localProcess = runtime.exec("su") val localOutputStream = localProcess.outputStream val localDataOutputStream = DataOutputStream(localOutputStream) localDataOutputStream.writeBytes(cmd) localDataOutputStream.flush() LogUtil.e("设备准备重启") } catch (e: IOException) { e.printStackTrace() } } fun rootInstallApk(file: File) { if (!sysHasRootPerssion()) { LogUtil.e("系统尚无Root权限") return } LogUtil.i("系统有Root权限") if (!file.exists()) { LogUtil.e("尚无APK文件") return } //静默安装 if (rootSlienceInstallApk(file.path)) { LogUtil.d("静默安装成功") } else { LogUtil.e("静默安装失败!!!") } } }

三、使用的是Android7.1已经root的,经过反复测试,是可以静默安装并且安装成功之后自动重启成功的;

四、单个应用,在安装成功app之后,这个App的进程都被杀掉了,根本收不到广播重启打开了,如果非要使用广播打开的话,需要另外做一个无UI的App,做成双守护进程,把版本升级任务都交给它,通过它重启打开已安装的App,广播代码如下;

class BootReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val action = intent.action intent.dataString?.substring(8)?.apply { LogUtil.d("BootReceiver onReceive():接收到Intent.getAction() = " + intent.action + " , 包名 = " + intent.dataString) /** * 接收安装广播 * android.intent.action.PACKAGE_ADDED */ if (action == Intent.ACTION_PACKAGE_ADDED) { LogUtil.d("BootReceiver onReceive():安装了:" + this + "包名的程序") } /** * 接收卸载广播 * android.intent.action.PACKAGE_REMOVED */ if (action == Intent.ACTION_PACKAGE_REMOVED) { LogUtil.d("BootReceiver onReceive():卸载了:" + this + "包名的程序") } /** * 接收更新广播 * android.intent.action.PACKAGE_REPLACED */ if (action == Intent.ACTION_PACKAGE_REPLACED) { LogUtil.d("BootReceiver onReceive():更新了:" + this + "包名的程序,context.getPackageName()=" + context.packageName) //更新的软件包名是否和我的工程一致 if (this == context.packageName) { //更新完后打开 val it = Intent() it.setClassName(this, "$this.xxxActivity") //启动类 it.action = "android.intent.action.MAIN" //首个启动类action it.addCategory("android.intent.category.LAUNCHER") //放入程序列表 //it.addCategory("android.intent.category.HOME"); //作为桌面,Home键打开,可做启动默认程序 it.addCategory("android.intent.category.DEFAULT") //隐式打开,如果没main可有,如果main可有可无 it.flags = Intent.FLAG_ACTIVITY_NEW_TASK context.startActivity(it) } } /** * 接收重启广播 * android.intent.action.PACKAGE_REPLACED */ if (action == Intent.ACTION_PACKAGE_RESTARTED) { LogUtil.d("BootReceiver onReceive():重启了:" + this + "包名的程序") } /** * 接收开机广播 * android.intent.action.BOOT_COMPLETED */ if (action == Intent.ACTION_BOOT_COMPLETED) { LogUtil.d("BootReceiver onReceive():仪器开机,开启了:" + this + "包名的程序") } } } }

五、在静默安装之后执行重启代码

/** * 执行此命令后:BootReceiver 监听到Intent.ACTION_PACKAGE_REPLACED,然后自启动 * */ fun rootStartApk(packageName: String, activityName: String): Boolean { val isSuccess = false var process: Process? = null try { process = Runtime.getRuntime().exec("su") val printWriter = PrintWriter(process.outputStream) printWriter.println("am start -n $packageName/.$activityName") printWriter.flush() printWriter.close() val value = process.waitFor() return returnResult(value) } catch (e: java.lang.Exception) { e.printStackTrace() } finally { process?.destroy() } return isSuccess }

 



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭