开发者

command line from within a program - not all commands are executed issue

I wrote some code that runs an android command line and collect the output.

it is executing "ls" correctly but when I put the command "top -n 1" it shows nothing.

is it a manifest issue? the phone is not rooted and when using "terminal emulator" I can see "top" output.

here is the code:

// ** execute command line and gather the output **//
    final StringBuilder log = new Strin开发者_StackOverflow社区gBuilder();
    try{
        ArrayList<String> commandLine = new ArrayList<String>();
        commandLine.add("top");
        commandLine.add("-n1");

        Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line;
        while ((line = bufferedReader.readLine()) != null){ 
            log.append(line);
            log.append(", \n"); 
        }
        log.append(", \n");
    } 
    catch (IOException e){

    } 

thanks, A.


You might want to show some of your code. Generally, commands you run using Runtime are not executed in a shell, so you might want to try something like "sh -c top -n 1" as the prog parameter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜