如何杀死由Android应用程序启动logcat的过程?

问题描述:

我的Andr​​oid应用程序,关于服务的启动实施,随后code:

I have Android application that on Service start implement followed code:

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

正如你看到我开始logcat的过程中监听日志。
不幸的是,一站式服务不停止logcat的后几次发射,我得到:

As you see I start logcat process to listen on logs. Unfortunately, the stop service doesn't stop logcat and after several launches 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  

... logcat的进程的列表。我怎么能杀了他们?

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

任何想法

既然你对过程实例的句柄,你应该能够使用 process.destroy()当你停止服务。该logcat的过程是你的应用程序的独立运行,因此他们将继续您的应用程序的服务运行后停止运行,除非你终止它们。

Since you have a handle on the Process instance, you should be able to use process.destroy() when you stop your service. The Logcat processes are running independently of your app, so they will continue running after your app's Service stops running unless you terminate them.

如果你只是想清理过程中您的设备上同时测试应用程序,您可以运行亚行外壳来获取设备的shell提示符,然后使用杀< PID方式> 终止进程

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

修改

过程文件还规定,你应该使用的ProcessBuilder 来设置和运行的过程:

The Process documentation also specifies that you should use ProcessBuilder to set up and run the process:

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