Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run adb shell commands from android code?

Tags:

android

I want to run these commands from java code:

adb shell settings put secure location_providers_allowed gps, wifi,network 

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

Please help me how to run from Android code.

like image 702
Getaw Dejen Avatar asked Jul 30 '15 18:07

Getaw Dejen


2 Answers

You can use this method to run commands

private void runShellCommand(String command) throws Exception {
    Process process = Runtime.getRuntime().exec(command);
    process.waitFor();
}
like image 116
Rami Jarrar Avatar answered Sep 24 '22 22:09

Rami Jarrar


OK you will be needing Runtime class. There is a good tutorial on executing shell commands here. For a quick answer, try this:

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);
like image 45
Behnam Avatar answered Sep 24 '22 22:09

Behnam