android root 权限自动安装并打开app

2022-11-16  本文已影响0人  微笑中的你

测试过了。有效的。。。当然必须是具有root权限的才能哦

path 是 apk的路径,

packageName 如 com.a.b

activityName 如 com.a.b.MainActivity

Kotlin 代码

    /**
     * 安装并打开app 
     */
    fun installAppAndOpenByCmd(path: String, packageName: String, activityName: String) {

        val cmd1: String = "am start -n $packageName/$activityName\n"
        val cmd2: String = "pm install -r $path && "
        val cmd = cmd2 + cmd1
        val runtime =  Runtime.getRuntime()
        try {
            val localProcess = runtime.exec("su")
            val localOutputStream = localProcess.outputStream
            val localDataOutputStream = DataOutputStream(localOutputStream)
            localDataOutputStream.writeBytes(cmd)
            localDataOutputStream.flush()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
上一篇下一篇

猜你喜欢

热点阅读