无法运行程序“adb”:错误=13,权限被拒绝。

9

我有一个程序,它必须只执行一个命令。

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Runtime.getRuntime().exec("adb shell input tap 0 0")
    }
}

但是我遇到了一个错误

Process: com.example.tomfo.pokerclicker, PID: 11578
java.io.IOException: Cannot run program "adb": error=13, Permission denied
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1050)
    at java.lang.Runtime.exec(Runtime.java:695)
    at java.lang.Runtime.exec(Runtime.java:525)
    at java.lang.Runtime.exec(Runtime.java:422)
    at com.example.tomfo.pokerclicker.MainActivity$onCreate$1.run(MainActivity.kt:14)
    at java.util.TimerThread.mainLoop(Timer.java:562)
    at java.util.TimerThread.run(Timer.java:512)
 Caused by: java.io.IOException: error=13, Permission denied
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:133)
    at java.lang.ProcessImpl.start(ProcessImpl.java:132)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    at java.lang.Runtime.exec(Runtime.java:695) 
    at java.lang.Runtime.exec(Runtime.java:525) 
    at java.lang.Runtime.exec(Runtime.java:422) 
    at com.example.tomfo.pokerclicker.MainActivity$onCreate$1.run(MainActivity.kt:14) 
    at java.util.TimerThread.mainLoop(Timer.java:562) 
    at java.util.TimerThread.run(Timer.java:512

虽然我已经将adb添加到Windows的属性路径中,并且可以通过Android Studio控制台运行abd命令(但在Android Studio控制台中,命令不是绿色的)。如何解决这个问题?此命令可以通过Studio控制台命令工作。


你是从模拟器还是从设备上执行这段代码的? - Skizo-ozᴉʞS ツ
从我的模拟器 - Strangelove
你是否执行了 adb root 并重启模拟器?然后执行 adb -e shell - Skizo-ozᴉʞS ツ
你在那个 adb 上有 x 权限吗? - Geno Chen
@Strangelove 有什么解决方案吗?因为我也遇到了 java.io.IOException: Cannot run program "adb": error=13, Permission denied 的问题。 - Ashish Jain
显示剩余2条评论
2个回答

15

adb是一种计算机上可以使用的工具,“adb shell”在设备(或模拟器)上打开一个shell,“adb shell command”在其上运行命令。

因此,如果您想在设备上以编程方式运行命令,只需删除“adb shell”即可:

Runtime.getRuntime().exec("input tap 0 0")

5
@ Cédric LE MOING,我按照您的回答使用了“adb shell wm overscan”命令,参数为“0,0,0,-220”,但它只是停止了错误13的异常消息出现,却没有执行任何操作。 - Anubhav Pandey

1
我是这样得到的
 fun commandAdb() {
    InstrumentationRegistry.getInstrumentation().uiAutomation.executeShellCommand("svc wifi disable")
    InstrumentationRegistry.getInstrumentation().uiAutomation.executeShellCommand("svc data disable")
}

还能用吗?因为Android Studio给我导入InstrumentationRegistry的错误,而executeShellCommand似乎在IDE的提示中不可见... - The_Long_Distance_Runner
谢谢。我能够使用你的答案来打开位置服务以测试位置。// 打开位置服务 InstrumentationRegistry.getInstrumentation() .uiAutomation .executeShellCommand("settings put secure location_mode 3") // 强制刷新 InstrumentationRegistry.getInstrumentation() .uiAutomation .executeShellCommand("settings get secure location_mode") - farid_z

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接