How do I run adb shell commands from Android code?

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 work with Android code.

+3


source to share


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();
}

      

+5


source


OK you need a class Runtime

. There is a good tutorial on how to execute 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);

      

+1


source







All Articles