How to kill a logcat process initiated by an Android application?

I have an Android application that implements the following code on Service start:

... Process process = Runtime.getRuntime().exec("logcat -v time -s " + arg); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream())); ... 

As you can see, I am running the logcat process to listen to the logs. Unfortunately, the stop service does not stop logcat and after several starts I get:

 app_125 4782 144 137124 30584 ffffffff 00000000 S com.wefi.agnt.ui app_125 4796 4782 1028 592 ffffffff 00000000 S logcat app_125 4839 4782 1076 640 ffffffff 00000000 S logcat app_125 4842 4782 952 516 ffffffff 00000000 S logcat app_125 4845 4782 896 460 ffffffff 00000000 S logcat app_125 4848 4782 900 464 ffffffff 00000000 S logcat app_125 4851 4782 876 440 ffffffff 00000000 S logcat shell 4852 171 788 336 c00cc7e4 afd0c78c S /system/bin/sh shell 4853 4852 944 332 00000000 afd0b83c R ps app_123 11189 144 114484 18908 ffffffff 00000000 S com.mspot.android.moviesClub.att 

... a list of logcat processes. How can I kill them?

Any ideas

+4
source share
2 answers

Since you have a handle to the Process instance, you can use process.destroy() when stopping the service. Logcat processes run independently of your application, so they will continue to work after the Service application stops working if you do not terminate them.

If you just want to clean up the processes on your device while testing the application, you can run adb shell to get a shell prompt on the device, and then use kill <PID> to complete the process.

EDIT

The Process documentation also indicates that you should use ProcessBuilder to configure and start the process:

 ProcessBuilder pb = new ProcessBuilder("logcat", "-v", "time", "-s", arg); pb.redirectErrorStream(true); Process process = pb.start(); ... 
+2
source

First, to start adb logging, create a process as:

  String cmd1 = "logcat -v time -f" + filename.getAbsolutePath(); Process logcat = Runtime.getRuntime().exec(cmd1); 

After that, when you want to stop adb updates to write to a file, just call:

  String cmd = "logcat -c"; try { logcat.destroy(); logcat = Runtime.getRuntime().exec(cmd); } catch (IOException e) { e.printStackTrace(); } 

This will destroy the previous build process and clear all previously written adb logs. So, the time you start ad logging again starts from the same time, without adding duplicate values ​​from the previous log.

+2
source

Source: https://habr.com/ru/post/943449/


All Articles