/**
* 每次记录日志之前先清除日志的缓存, 不然会在两个日志文件中记录重复的日志
*/
private void clearLogCache()
{
Process proc = null;
List<String> commandList = new ArrayList<String>();
commandList.add("logcat");
commandList.add("-c");
try
{
proc = Runtime.getRuntime().exec(commandList.toArray(new String[commandList.size()]));
StreamConsumer errorGobbler = new StreamConsumer(proc.getErrorStream());
StreamConsumer outputGobbler = new StreamConsumer(proc.getInputStream());
errorGobbler.start();
outputGobbler.start();
if (proc.waitFor() != 0)
{
Log.e(TAG, " clearLogCache proc.waitFor() != 0");
recordLogServiceLog("clearLogCache clearLogCache proc.waitFor() != 0");
}
} catch (Exception e)
{
Log.e(TAG, "clearLogCache failed", e);
recordLogServiceLog("clearLogCache failed");
} finally
{
try
{
proc.destroy();
} catch (Exception e)
{
Log.e(TAG, "clearLogCache failed", e);
recordLogServiceLog("clearLogCache failed");
}
}
}
生词 Gobble 解释 to eat something very fast
java.lang.Process.getErrorStream
public abstract InputStream getErrorStream ()
Returns an input stream that is connected to the error stream (stderr) of the native process represented by this object.
java.lang.Process.getInputStream
public abstract InputStream getInputStream ()
Returns an input stream that is connected to the standard output stream (stdout) of the native process represented by this object.
java.lang.Process.waitfor
public abstract int waitFor ()
Causes the calling thread to wait for the native process associated with this object to finish executing.
Returns
the exit value of the native process being waited on.
Throws
InterruptedException if the calling thread is interrupted.
java.lang.Process.destroy
public abstract void destroy ()
Terminates this process and closes any associated streams.
class StreamConsumer extends Thread
{
InputStream is;
List<String> list;
StreamConsumer(InputStream is)
{
this.is = is;
}
StreamConsumer(InputStream is, List<String> list)
{
this.is = is;
this.list = list;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
{
if (list != null)
{
list.add(line);
}
}
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}