美文网首页
安卓静默安装、卸载APK kotlin

安卓静默安装、卸载APK kotlin

作者: 奈蜇 | 来源:发表于2024-04-22 16:15 被阅读0次

    应用要求:sdk>21、系统签名、android:sharedUserId="android.uid.system"

    代码

    //静默安装
        private const val ACTION_INSTALL = "com.huopai.ACTION_INSTALL"
        private suspend fun silentInstall(context: Context, apkPath: String): Boolean {
            try {
                return withContext(Dispatchers.IO) {
                    val file = File(apkPath)
                    if (file.isFile) {
                        val packageManager = context.packageManager
                        val packageInstaller = packageManager.packageInstaller
                        val sessionParams = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
                        val sessionId = packageInstaller.createSession(sessionParams)
                        val session = packageInstaller.openSession(sessionId)
                        val outputStream = session.openWrite("PackageInstaller", 0, file.length())
                        val inputStream = FileInputStream(file)
                        inputStream.copyTo(outputStream)
                        session.fsync(outputStream)
                        //关闭流
                        outputStream.close()
                        inputStream.close()
                        //监听结果
                        val deferredResult = CompletableDeferred<Int>()
                        val receiver = object : BroadcastReceiver() {
                            override fun onReceive(context: Context, intent: Intent) {
                                XLog.d("收到广播 $intent")
                                val status: Int = intent.extras?.getInt(PackageInstaller.EXTRA_STATUS, -1) ?: -1
                                //异步拿结果
                                deferredResult.complete(status)
                            }
                        }
                        //注册广播
                        val intentFilter = IntentFilter()
                        intentFilter.addAction(ACTION_INSTALL)
                        ContextCompat.registerReceiver(context, receiver, intentFilter, ContextCompat.RECEIVER_NOT_EXPORTED)
                        //提交安装
                        val intent = Intent(ACTION_INSTALL)
                        val pendingIntent = PendingIntent.getBroadcast(context, sessionId, intent, PendingIntent.FLAG_UPDATE_CURRENT)
                        session.commit(pendingIntent.intentSender)
                        //等待安装结果
                        val result = withTimeoutOrNull(10000) {
                            deferredResult.await()
                        }
                        //反注册
                        context.unregisterReceiver(receiver)
                        return@withContext if (result == null) {
                            //超时  一般这里需要释放任务(已反注册)
                            false
                        } else {
                            result == PackageInstaller.STATUS_SUCCESS
                        }
                    }
                    false
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return false
        }
    

    卸载apk

    //静默卸载APK
        private const val ACTION_UNINSTALL = "com.huopai.ACTION_UNINSTALL"
        suspend fun silentUninstallApk(context: Context, packageName: String): Boolean {
            try {
                val deferredResult = CompletableDeferred<Int>()
                val receiver = object : BroadcastReceiver() {
                    override fun onReceive(context: Context, intent: Intent) {
                        XLog.d("收到广播 $intent")
                        val status: Int = intent.extras?.getInt(PackageInstaller.EXTRA_STATUS, -1) ?: -1
                        //异步拿结果
                        deferredResult.complete(status)
                    }
                }
                //注册广播
                val intentFilter = IntentFilter()
                intentFilter.addAction(ACTION_UNINSTALL)
                ContextCompat.registerReceiver(context, receiver, intentFilter, ContextCompat.RECEIVER_NOT_EXPORTED)
                //开始卸载
                val intent = Intent(ACTION_UNINSTALL)
                val pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
                val packageInstaller = context.packageManager.packageInstaller
                packageInstaller.uninstall(packageName, pendingIntent.intentSender)
                //等待卸载结果
                val result = withTimeoutOrNull(6000) {
                    deferredResult.await()
                }
                //反注册
                context.unregisterReceiver(receiver)
                return if (result == null) {
                    //超时  一般这里需要释放任务(已反注册)
                    false
                } else {
                    result == PackageInstaller.STATUS_SUCCESS
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return false
        }
    

    相关文章

      网友评论

          本文标题:安卓静默安装、卸载APK kotlin

          本文链接:https://www.haomeiwen.com/subject/zzxixjtx.html