在Android上通过java代码运行Shell命令? [英] Running Shell commands though java code on Android?

查看:39
本文介绍了在Android上通过java代码运行Shell命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它应该使用一些 shell 命令将文件从 sdcard 复制到/system/media/.它将需要 root,我正在一个有 root 权限的设备上进行测试.我正在使用运行时来执行 shell 命令,但它不起作用.这是我的运行时所拥有的

I've got an app that's supposed to use some shell commands to copy a file from the sdcard to /system/media/. It will require root, and I am testing on a rooted device. I'm using runtimes to execute the shell commands, but it's not working. Here's what I've got for my runtimes

public void RunAsRoot{String[] commands = {"sysrw", "rm /data/local/bootanimation.zip", "sysro"};{
    Process p = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(p.getOutputStream());            
    for (String tmpCmd : commands) {
    os.writeBytes(tmpCmd+"
");
    }           
    os.writeBytes("exit
");  
    os.flush();
}

但我的 logcat 只显示其中两个没有被拒绝

But my logcat only shows two of them not getting rejected

07-30 03:14:11.112: WARN/su(3593): request rejected (10047->0 /system/bin/sh)
07-30 03:14:11.132: DEBUG/su(3592): 10047 com.bba.stormdroid executing 0 /system/bin/sh using shell /system/bin/sh : sh
07-30 03:14:11.152: WARN/su(3594): request rejected (0->0 /system/bin/sh)
07-30 03:14:11.182: WARN/su(3595): request rejected (0->0 /system/bin/sh)
07-30 03:14:11.202: WARN/su(3596): request rejected (0->0 /system/bin/sh)
07-30 03:14:11.242: DEBUG/su(3597): 10047 com.bba.stormdroid executing 0 /system/bin/sh using shell /system/bin/sh : sh

这两个看起来是 sysrw 和 sysro 命令,但当我触发此代码时,应用程序仍然要求获得 root 权限.我是使用根目录的新手,我似乎无法弄清楚如何让它工作.

Those two look to be the sysrw and sysro commands, yet the app still asks for root permission when I trigger this code. I'm new to working with root stuff and I can't seem to figure out how to get this to work.

推荐答案

要运行 root 命令,您必须使用以下格式:

To run root commands, you have to use the flllowing format:

    public void RunAsRoot(String[] cmds){
            Process p = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(p.getOutputStream());            
            for (String tmpCmd : cmds) {
                    os.writeBytes(tmpCmd+"
");
            }           
            os.writeBytes("exit
");  
            os.flush();
}

传入一个字符串数组的地方,每个字符串都是一个需要执行的命令.例如:

where you pass in an array of strings, each string being a command that needs to be executed. For example:

String[] commands = {"sysrw", "rm /data/local/bootanimation.zip", "sysro"};

这篇关于在Android上通过java代码运行Shell命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆