简体   繁体   English

是否可以通过我的 android 应用程序执行 adb 命令?

[英]Is it possible to execute adb commands through my android app?

Can anyone say, whether adb commands can be executed through my android application.任何人都可以说,是否可以通过我的android应用程序执行adb命令。 If it is possible to execute, how it can be implemented?如果可以执行,如何执行?

You can do it with this:你可以这样做:

Process process = Runtime.getRuntime().exec("your command");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));

Don't forget to surround it with a try and catch statement.不要忘记用 try 和 catch 语句包围它。

Edit:编辑:

@Phix is right, ProcessBuilder would be better to use. @Phix 是对的,使用ProcessBuilder会更好。

Normal Android apps have different privileges to processes started via adb , eg, processes started via adb are allowed to the capture the screen whereas normal apps aren't.普通的 Android 应用程序对通过adb启动的进程有不同的权限,例如,通过adb启动的进程可以捕获屏幕,而普通应用程序则不能。 So, you can execute commands from your app via Runtime.getRuntime().exec() , but they won't have the same privileges as if you had executed from an adb shell .因此,您可以通过Runtime.getRuntime().exec()从您的应用程序执行命令,但它们不会具有与您从adb shell执行时相同的权限。

i came across this post looking for a different query, but i have worked specifically with input on android before, so I'd just like to put some clarity on the matter.我在这篇文章中找到了一个不同的查询,但我之前专门处理过 android 上的input ,所以我想澄清一下这个问题。

The reason why之所以

Runtime.getRuntime().exec("adb shell input keyevent 120");    

Is not working, is because you are not removing不工作,是因为你没有删除

adb shell   

The ADB part is only for use on your computer, if you have incorrectly installed ADB, the command would actually be a path to the adb.exe file on your computer, like this ADB部分仅在您的计算机上使用,如果您错误地安装了 ADB,该命令实际上是您计算机上 adb.exe 文件的路径,如下所示

C:\XXXX\ADB Files\adb.exe shell    

or要么
C:\\XXXX\\ADB Files\\adb shell C:\\XXXX\\ADB Files\\adb shell

The shell part tells the ADB program on your computer to access the devices shell, so your device will not know what shell is either... shell部分告诉您计算机上的 ADB 程序访问设备外壳,因此您的设备也不知道外壳是什么......

Using sh /path/to/commandList.sh will execute the commads listed in commandList.sh as it is a shell script (a .batch file on windows is similar )使用sh /path/to/commandList.sh将执行 commandList.sh 中列出的命令,因为它是一个 shell 脚本(Windows 上的 .batch 文件类似)

The command you want to use is您要使用的命令是

Runtime.getRuntime().exec("input keyevent 120");     

However this will cause Environment null and working directory null, you can bypass this by writing the commands to a shell script ( .sh file ) and then running the script with但是,这会导致环境为 null 和工作目录为 null,您可以通过将命令写入 shell 脚本(.sh 文件)然后使用以下命令运行脚本来绕过此问题

Runtime.getRuntime().exec("sh path/to/shellScript.sh");   

Sometimes the sh is not needed, but i use it just incase.有时不需要sh ,但我只是为了以防万一。

I hope this clears at least something up :)我希望这至少可以解决一些问题:)

adb shell invoked in Runtime.getRuntime().exec is not running under shell user.在 Runtime.getRuntime().exec 中调用的 adb shell 不在 shell 用户下运行。 It provide shell but with same process owner user (like u0_a44).它提供外壳,但具有相同的进程所有者用户(如 u0_a44)。 That's the reason all command did not work.这就是所有命令不起作用的原因。

Executing执行

Runtime.getRuntime().exec("adb shell input keyevent 120");

I got the following error: java.io.IOException: Cannot run program "adb": error=13, Permission denied.我收到以下错误:java.io.IOException:无法运行程序“adb”:错误=13,权限被拒绝。

Executing执行

Runtime.getRuntime().exec("adb shell input keyevent 120");

There is no error but at the same time, my request is not processed to take the screenshot.没有错误,但同时,我的请求未处理以截取屏幕截图。

I found out this was working in earlier versions of android but later it was removed.我发现这在早期版本的 android 中有效,但后来被删除了。 Though I'm not able to provide the source here why it is not working.虽然我无法在这里提供来源,但为什么它不起作用。

Hope this helps someone like me who is trying to use this approach to take the screenshot when the app is not in the foreground.希望这可以帮助像我这样尝试使用这种方法在应用程序不在前台时截取屏幕截图的人。

This is what I do in Kotlin, I also get command responses too这就是我在 Kotlin 中所做的,我也得到命令响应

fun runShellCommand(command: String) {
    // Run the command
    val process = Runtime.getRuntime().exec(command)
    val bufferedReader = BufferedReader(
        InputStreamReader(process.inputStream)
    )

    // Grab the results
    val log = StringBuilder()
    var line: String?
    line = bufferedReader.readLine()
    while (line != null) {
        log.append(line + "\n")
        line = bufferedReader.readLine()
    }
    val Reader = BufferedReader(
        InputStreamReader(process.errorStream)
    )

    // if we had an error during ex we get here
    val error_log = StringBuilder()
    var error_line: String?
    error_line = Reader.readLine()
    while (error_line != null) {
        error_log.append(error_line + "\n")
        error_line = Reader.readLine()
    }
    if (error_log.toString() != "")
        Log.info("ADB_COMMAND", "command : $command $log error $error_log")
    else
        Log.info("ADB_COMMAND", "command : $command $log")
}

Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
string cmd = "/system/bin/input keyevent 23\n";
os.writeBytes(cmd);

the phone must be rooted.手机必须root。 here I have executed adb command "input keyevent 23".在这里我执行了 adb 命令“input keyevent 23”。 remember when you execute adb command through su you does not need to add "adb shell input keyevent 23"请记住,当您通过 su 执行 adb 命令时,您不需要添加“adb shell input keyevent 23”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM