Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从android代码运行adb shell命令?_Android - Fatal编程技术网

如何从android代码运行adb shell命令?

如何从android代码运行adb shell命令?,android,Android,我想从java代码中运行以下命令: adb shell settings put secure location_providers_allowed gps, wifi,network a broadcast -a android.location.GPS_ENABLED_CHANGE --ez enabled true 请帮助我如何从Android代码运行。您可以使用此方法运行命令 private void runShellCommand(String command) throws E

我想从java代码中运行以下命令:

adb shell settings put secure location_providers_allowed gps, wifi,network 

a broadcast -a android.location.GPS_ENABLED_CHANGE --ez enabled true

请帮助我如何从Android代码运行。

您可以使用此方法运行命令

private void runShellCommand(String command) throws Exception {
    Process process = Runtime.getRuntime().exec(command);
    process.waitFor();
}

好的,您将需要
运行时
课程。关于执行shell命令有一个很好的教程。要快速获得答案,请尝试以下方法:

String commandToRun = "adb shell settings put secure location_providers_allowed gps, wifi,network a broadcast -a android.location.GPS_ENABLED_CHANGE --ez enabled true";
Runtime.getRuntime().exec(commandToRun);

那么你想运行shell命令吗?是的,我想运行shell命令以启用gpsI使用此[link]()来运行命令,当我在pc上使用带cmd的adb shell命令进行测试时,它可以正常工作,但当我从我的应用程序运行命令时,它不能正常工作,我使用kitkat(4.4.4)并且我想以编程方式启用gps(从应用程序运行命令)知道这个错误“java.io.IOException:无法运行程序“adb”:错误=13,权限被拒绝了吗?@HaiHw您解决过这个io异常吗?我得到了同样的结果。@EugenioLopez我认为它需要根设备才能运行。没有根设备不能解决问题@EugenioLopez