Any way to run shell commands on android programmatically?
Check out Log Collector as an example. Here is the relevant file.
The key is here:
ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add("logcat");//$NON-NLS-1$
[...]
Process process = Runtime.getRuntime().exec(commandLine);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
executing shell commands programmatically in Android
exec
in Java starts a new process. So the first line makes a new su
process, which is going to simply sit there and wait for your input. The second line starts a new dhcpcd
process, which won't be privileged and so won't produce useful output.
What you want is to run dhcpcd
using su
, typically like this:
exec("su -c dhcpcd eth0")
Execute shell commands and get output in a TextView
You can run command and display command output into text as below :
public class MainActivity extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.cmdOp);
tv.setText("Output :"+"\n"+runAsRoot());
}
public String runAsRoot() {
try {
// Executes the command.
Process process = Runtime.getRuntime().exec("ls -l");
// Reads stdout.
// NOTE: You can write to stdin of the command using
// process.getOutputStream().
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
// Waits for the command to finish.
process.waitFor();
return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Note : The "su" command does only run if the device is rooted. Otherwise it throws an exception.
How to run cmd/shell commands from android studio programmatically?
You can't execute ADB directly from an android device.
You'll need to write a desktop application which executes the command and can be called by the android application, or (easier) use an automation software to call remote commands from your phone.
Run shell commands from android program
You can't simply run 'su' on the emulator, there's no root access by default. You'll need to install the 'su' program as well as the SuperUser.apk, and you'll have to do this each time you start the emulator unless using snapshots.
More information and links to the files you need can be found here on SO as well as this blog post by Russell Davis
Related Topics
How Does Push Notification Technology Work on Android
Bind Service to Activity in Android
How to Define Different Dependencies for Different Product Flavors
In Android App Toolbar.Settitle Method Has No Effect - Application Name Is Shown as Title
How to Make an Android App to Always Run in Background
Best Way to Have Paid and Free Version of an Android App
How to Size an Android View Based on Its Parent's Dimensions
Android: How to Convert String to Date
Install_Failed_Update_Incompatible When I Try to Install Compiled .Apk on Device
Google Android Usb Driver and Adb
Display Fragment Viewpager Within a Fragment
Use a Custom Contextual Action Bar for Webview Text Selection
Get Uri of .Mp3 File Stored in Res/Raw Folder in Android