I'm trying to get the radio logs from Android device. I've found this snippet to read radio logs. However, the control never enters the while loop.
try {
String line = "";
Process process = Runtime.getRuntime().exec("adb logcat -b radio");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
Log.i("entered loop", log.toString());
log.append(line + "\n");
}
Log.i("app", log.toString());
} catch (IOException e) {}
The code works fine if we replace the command as adb logcat -d as parameter to exec().
I think the control is stuck in the line = bufferedReader.readLine() statement in condition.
EDIT:
Basically, I want to read the notification that is displayed on the screen. For example: The notification that we see after the call or sending SMS displaying remaining balance.
The notification is actually printed in the Android Logs and thus I am reading Logs and extracting the message from it. Is there any other way to store all notifications displayed on screen.
Please help me in this regard.
If you're running this code on a device, try replacing
Process process = Runtime.getRuntime().exec("adb logcat -b radio");
with
Process process = Runtime.getRuntime().exec("/system/bin/sh" + "-c" + "adb logcat -b radio");
otherwise, try replacing it with (on linux)
Process process = Runtime.getRuntime().exec("/bin/sh" + "-c" + "adb logcat -b radio");
or (on windows)
Process process = Runtime.getRuntime().exec("C:\\Windows\\System32\\cmd.exe" + "-c" + "adb logcat -b radio");
Related
I am trying to start internal logs within my android devices without having to go directly into the adb shell. I have to script this process so I can run it from a program.
I am aware that you can take the following steps to start internal logs on an android device:
Open a cmd prompt
enter 'adb shell'
enter 'logcat -v time -f /sdcard/LogFile.txt&'
The above will start a logcat process within the actual device. I can now unplug my phone from the computer and move around then come back and collect the logs once my test is complete. It's crucial that I am able to start this process and be able to unplug my device with the logs still running.
To my knowledge running 'adb shell' in front of any command would run as if it were in the shell. Therefore by this logic I tried running:
Method 1:
'adb shell logcat -v time -f /sdcard/LogFile.txt&'
This command did properly start the log on the device which is great. However, once I unplug from my computer the logcat process stops.
Method 2:
'adb shell "logcat -v time -f /sdcard/LogFile.txt&" '
This didn't seem to do anything at all on the phone and I don't know why.
Method 3
I have tried the scripting method as well where I run a Batch file that contains only:
'adb shell < Commands.txt'
Where commands has the single line:
'logcat -v time -f /sdcard/LogFile.txt&'
This doesn't appear to do anything. It appears to send the command once the window comes up but doesn't actually perform the action.
Any help on this topic would be greatly appreciated. Thanks.
you can use this code in application class. so when app will launch this code will start executing. and it will create new log file according to current time.
Thread t = new Thread(new Runnable() {
public void run() {
while (collectLog == true) {
try {
Thread.sleep(1000 * 60 * 6);
StringBuilder log = null;
Date now = new Date();
String fileName = formatter.format(now);
File file = new File(dir, fileName + "_logcat2.txt");
try {
Process process = Runtime.getRuntime().exec("logcat -d");// d will dump logs
//Process process = Runtime.getRuntime().exec("logcat -c"); c will clear logs
// process = Runtime.getRuntime().exec("logcat -f " + file);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
log.append("\n");
}
} catch (IOException e) {
}
try {
//to write logcat in text file
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut)
// Write the string to the file
osw.append(log.toString());
osw.flush();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();
Hi everyone I recently wrote some code to read LogCat output continuously within a background Thread started by a background Service in my app. I do not think there are errors in my code but it's not working.
In my Thread, I have:
#Override
public void run() {
Process process = null;
String cmd = "logcat -v time | grep 'AudioFocus requestAudioFocus()'";
try {
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("logcat -c" + "\n");
os.writeBytes(cmd + "\n");
os.flush();
} catch(Exception e) {
}
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
try {
while ( (line = reader.readLine()) != null ) {
Log.e(StaticVal.TAG, line);
// code for write line to my own log file
}
} catch(Exception e) {
}
}
I also added permission to manifest:
<uses-permission android:name="android.permission.READ_LOGS" />
However, when I plug my device with PC and test it with Android Studio, Log.e(StaticVal.TAG, line); did not print out the results and nothing wrote into my own log file.
Any help is appreciated!
(Directly run logcat -v time | grep 'AudioFocus requestAudioFocus()' on my device through adb terminal get the results as expected)
I'm doing a simple app that show the currently process in android, like a shell.
My app execute ls, cd, makedir and other commands, but top or htop command doesnt. (htop doesn't recognize, and top, the app freeze). I need root to this? I've downloaded the terminal app in unrooted android and top command works.
my app has 2 class. a principal and a shell
principal class
public void onClick(View arg0) {
// TODO Auto-generated method stub
ShellExecuter exe = new ShellExecuter();
command = input.getText().toString();
String outp = exe.Executer(command);
out.setText(outp);
Log.d("Output", outp);
}
shell class
public String Executer(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
String response = output.toString();
return response;
}
Why some commands the app works and top for example doesn't??
If you have your Device connected, go to the shell to see what commands you have available using the following command in your computer's command line:
adb shell
At first glance you will be able to tell that "top" is a job that "displays and update sorted information about processes" and it blocks the shell commands line, hence, in your application is locking the thread that executed that command.
Hope it Helps!
Regards!
thanks. adb shell with top command show the process
but
p = Runtime.getRuntime().exec("top");
doesn't maybe works??
I don't understante cause my app freeze
or a top -n 1 not fixed?
I try to execute "am profile start " within my Android app to profile another PID. From ADB everything works fine, for example: adb shell am profile 5397 start /sdcard/my.app.trace works. But trying to execute "am" or "pm", or anything that executes app_process gives the following error:
/system/bin/am[8]: app_process: can't execute: Permission denied
It also does not work from a terminal emulator, same error. If I am executing it (both on adb or on device) with root the Segmentation Fault like here occurs: Segmentation Fault executing pm on android. The answers there did not solve my problem with accessing "am" with root, however I would like to solve the problem to run the command without root. There is a problem with executing app_process on the device which I am not able to solve. I am using a rooted Nexus 5 with the Android L Preview. With my old rooted Motorola Milestone the commands run on the terminal emulator. Here is the code for just trying to execute the "am" command within my app which should list the options of that command but produces the error above.
try {
String outputString;
String command = "am";
Log.d(TAG, "EXECUTING CMD: " + command);
// execute command and read output
process = Runtime.getRuntime().exec(command);
BufferedReader stdError = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
process.getInputStream()));
while ((outputString = stdInput.readLine()) != null) {
Log.d(TAG, " EXECUTING CMD Here is the standard output of the command (if any):\n");
Log.d(TAG, outputString);
}
while ((outputString = stdError.readLine()) != null) {
Log.d(TAG, "EXECUTING CMD Here is the standard error of the command (if any):\n");
Log.d(TAG, outputString);
}
Log.d(TAG, "****** / " + sCmd + " *******");
} catch (IOException exception) {
exception.printStackTrace();
}
I wrote some code that runs an android command line and collect the output.
it is executing "ls" correctly but when I put the command "top -n 1" it shows nothing.
is it a manifest issue? the phone is not rooted and when using "terminal emulator" I can see "top" output.
here is the code:
// ** execute command line and gather the output **//
final StringBuilder log = new StringBuilder();
try{
ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add("top");
commandLine.add("-n1");
Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null){
log.append(line);
log.append(", \n");
}
log.append(", \n");
}
catch (IOException e){
}
thanks,
A.
You might want to show some of your code. Generally, commands you run using Runtime are not executed in a shell, so you might want to try something like "sh -c top -n 1" as the prog parameter.