首页 > 解决方案 > 如何以编程方式从 android studio 运行 cmd/shell 命令?

问题描述

我已经运行了以下代码行和一些类似的解决方案,但似乎没有任何效果:

    StringBuilder cmdOut = new StringBuilder();
    Process process;
   try {
       process = Runtime.getRuntime().exec("adb shell");
       InputStreamReader r = new 
       InputStreamReader(process.getInputStream());
       BufferedReader bfreader = new BufferedReader(r);
       char [] buf  = new char[4096];
       int nRead = 0;
       while((nRead = bfreader.read(buf)) > 0)
       {
           cmdOut.append(buf,0,nRead);
       }
       bfreader.close();
       try{
           process.waitFor();
       }
       catch (InterruptedException e)
       {
           e.printStackTrace();
       }
   }
   catch (IOException e)
   {
       e.printStackTrace();
   }


but when i run this i got the following error:I have looked after every answer but I have not found any working solution 

java.io.IOException: Cannot run program "adb": error=13, Permission denied

如果有人有任何可行的解决方案,请与示例分享。

标签: javaandroid-studio

解决方案


您不能直接从 android 设备执行 ADB。

您需要编写一个桌面应用程序来执行命令并且可以由 android 应用程序调用,或者(更容易)使用自动化软件从您的手机调用远程命令。


推荐阅读