How to know whether service is running using adb shell in android
Try the command line
adb shell service list
I get a list of service names and their package names as well.
How do I listen for a response from shell command in android studio?
UPDATE : Solution is in Unable using Runtime.exec() to execute shell command "echo" in Android Java code :
Runtime.getRuntime.exec()
doesn't execute a shell command directly,
it executes an executable with arguments. "echo" is a builtin shell
command. It is actually a part of the argument of the executable sh
with the option -c. Commands likels
are actual executables. You can
usetype echo
andtype ls
command in adb shell to see the
difference.So final code is:
String[] cmdline = { "sh", "-c", "echo $..." };
Runtime.getRuntime().exec(cmdline);
cat
is also executable from within Runtime.exec()
without invoking sh
This is also analyzed in https://www.javaworld.com/article/2071275/when-runtime-exec---won-t.html?page=2 in paragraph Assuming a command is an executable program
The code in Execute shell commands and get output in a TextView is good although it uses a command that is executable directly (ls
, see update above) :
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);
}
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.
Android ADB commands to get the device properties
adb shell getprop ro.build.version.sdk
If you want to see the whole list of parameters just type:
adb shell getprop
Related Topics
How to Fix Error: No Signature of Method: Build_Ap86Oam3Dut3Pxce3X49Rdtma.Android()
Progressbar Togther with Asynctask
How to Use Autocompletetextview and Populate It with Data from a Web API
Replace One Fragment with Another in Viewpager
How to Switch Between Hide and View Password
Select All Text Inside Edittext When It Gets Focus
Catch on Swipe to Dismiss Event
The Import Android.Support.V7 Cannot Be Resolved
Remove Line Break in Tablayout
Supporting Multiple Screen Size - Android
How to Change the Launcher Logo of an App in Android Studio
Ssl Proxy/Charles and Android Trouble
Custom Ssl Handling Stopped Working on Android 2.2 Froyo
Gatt Callback Fails to Register