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

测试过了。有效的。。。当然必须是具有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()
        }
    }

你可能感兴趣的:(android root 权限自动安装并打开app)