I am trying to get the output of android shell command 'getprop' with java since getprop() always returns null no matter what.
I tried this from developer.android.com:
Process process = null;
try {
process = new ProcessBuilder()
.command("/system/bin/getprop", "build.version")
.redirectErrorStream(true)
.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream in = process.getInputStream();
//String prop = in.toString();
System.out.println(in);
process.destroy();
However what is printed is not the output but a bunch of characters and numbers (dont have the exact output right now).
How can i get the output of the process?
Thanks!
Is there any particular reason why you want to run the command as an external process?
There is a simpler way:
String android_rel_version = android.os.Build.VERSION.RELEASE;
However, if you really want to do it via a shell command, here is the way I got it to work:
try {
// Run the command
Process process = Runtime.getRuntime().exec("getprop");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
// Grab the results
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line + "\n");
}
// Update the view
TextView tv = (TextView)findViewById(R.id.my_text_view);
tv.setText(log.toString());
} catch (IOException e) {
}
Related
I have connected three cameras to my phone and i want to know their ip addresses. In windows and linux its with the arp command but this command does not run on the android terminal . How can i run arp command on android or it there any other way to achieve the same?
use this code work for me.
try {
Process process = Runtime.getRuntime().exec("cat /proc/net/arp");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
String output = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
Log.d("*************", ""+output);
}
catch (IOException e) {
e.printStackTrace();
System.out.println(e);
}
return "";
I am using logcat -d to flush logs to a file programmatically. Below is the piece of code I wrote in the onCreate() to do the same:
Log.d(TAG, "First");
Log.d(TAG, "Second");
Log.d(TAG, "Third");
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
}
PrintWriter printWriter = new PrintWriter(new FileWriter(FILE_PATH));
printWriter.println(log.toString());
printWriter.close();
TextView tv = (TextView)findViewById(R.id.textView);
tv.setText(log.toString());
} catch (IOException e) {
}
The logs are not flushed completely. Sometimes, it dumps First, Second and Third, while at other times, it shows only few of the three. The worst case is, there are times when it dumps none.
To check if there are any File write issues, I also used TextView. Even the textView follows the same behavior.
Why logcat -d is behaving unexpectedly?
Restart your adb and try to see directly the ddms logcat.
I want to set two commands in one process on android.
I know that nativeProcess = Runtime.getRuntime().exec("su"); but I want to add whoami in the same process. This is possible?
try this one:
public static void go()
{
String[] command = {"/system/bin/sh", "-c", "ps | grep apps ; ls"};
ProcessBuilder processBuilder = new ProcessBuilder(command);
try {
Process process =processBuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:\n",
Arrays.toString(command));
while ((line = br.readLine()) != null) {
System.out.println(line);
Log.e("output of go-->", "==>"+line);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
su -c "command" will run your command as superuser (something like sudo in Debian based systems)
i want to get logcat data programmatically i used the below code
StringBuilder debuglog=new StringBuilder();
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
debuglog.append(line);
debuglog.append("\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
Above code is working fine
This line "logcat -d" shows -d,-w,-i messages
But,my problem is i want to show -e messages
To get only ERROR use this:
Process process = Runtime.getRuntime().exec("logcat -d *:E");
All I want to achieve is to get the response from the execution.
For example if I execute command like this "ls"
I want to get string with all the files and directories
For example like this
Runtime.getRuntime().exec("ls");
But I do not know how to get the response.
I run something like this, I thought that I will redirect the output but still nothing
Runtime.getRuntime().exec("ls",null,new File("/sdcard/myFile") );
I tried also something like this but still nothing
Runtime.getRuntime().exec("echo | ls > /sdcard/myfile");
Any ideas ?
Have you tried this?
String cmd = "commands";
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()).length() > -1)
{
log.append(line);
}
try this code
try {
String[] commands = {"dumpstate > /sdcard/log1.txt"};
Process p = Runtime.getRuntime().exec("/system/bin/sh -");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : commands) {
os.writeBytes(tmpCmd+"\n");
}
} catch (IOException e) {
e.printStackTrace();
}