13

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.

2
  • so you want to run shell commands? Commented Jul 30, 2015 at 19:03
  • yes I want to run shell commands to enable gps Commented Jul 30, 2015 at 19:13

2 Answers 2

9

You can use this method to run commands

private void runShellCommand(String command) throws Exception {
    Process process = Runtime.getRuntime().exec(command);
    process.waitFor();
}
Sign up to request clarification or add additional context in comments.

5 Comments

any idea of this error "java.io.IOException: Cannot run program "adb": error=13, Permission denied" ?
@HaiHw Have you ever solved that io exception? I'm getting the same thing.
@EugenioLopez I think it requires rooted device to run.
No rooted device won't solve the problem @Eugenio Lopez
@HaiHw -> just write the command it self : for example :use "settings put secure location_providers_allowed" instead of "adb shell settings put secure location_providers_allowed..."
4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.