How to write adb shell commend in java.lang.Runtim

2019-04-02 01:35发布

I need to use adb connect pc to device and do some checking. So I try to use java.lang.Runtime.getRuntime().exec(cmd) to get adb shell result into my program. But I don't know how to write the adb shell commend in the exec call, something like:

String cmd =adkHome + "adb.exe -s " + device + " shell ls";

then cd data/app

How do I do this?

标签: java android adb
2条回答
别忘想泡老子
2楼-- · 2019-04-02 02:08

If you mean to run this on the phone from your java app, you just need:

String cmd = "ls";
查看更多
Viruses.
3楼-- · 2019-04-02 02:20

This may be what you're looking for?

    
    String cmd = "adb shell ls";
    String cmdreturn = "";
    Runtime run = Runtime.getRuntime();
    Process pr = run.exec(cmd);
    pr.waitFor();
    BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
    while ((line=buf.readLine())!=null) {
         System.out.println(cmdreturn);
    }
    
    

As far as preforming actions in a shell, I would recommend writing a shell script that you execute. In this case instead of

    
    String cmd = "adb shell ls";
    
    
Replace it with
    
    String cmd = "shellscript.sh";
    
    

Cheers!

查看更多
登录 后发表回答