I have a button and on its click I am displaying a logcat in a textview.
Button click event -
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Thread mThread = new MonitorLogThread();
mThread.start();
}
The thread that does the execution -
private class MonitorLogThread extends Thread
{
public MonitorLogThread()
{
Process process;
try {
process = Runtime.getRuntime().exec("logcat UserFragment:I
LookUpActivity:I IncomingStream:I *:S");
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
Log.i(TAG, "Getting the logs for Logcat");
logItems.clear();
String line;
// Check if it matches the pattern
while(((line=br.readLine()) != null) && !this.isInterrupted() && isMonitor){
// Filter for your app-line
if (line.contains("ClickedEvents:")){
System.out.println("the line added......"+line);
logItems.add(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
The problem is first time I click button the lines in textview (and logcat)
are shown as -
the line added......User click event
the line added......User click event success
the line added......User click event done
the line added......User click event gone
But second time when I click button the lines in textview (and logcat)
are shown as -
the line added......User click event
the line added......User click event
the line added......User click event success
the line added......User click event success
the line added......User click event done
the line added......User click event done
the line added......User click event gone
the line added......User click event gone
The third time it is added further more and so on....
So the number of times I click, it duplicates it.
What may be the issue ?
You're getting duplicates because the process is never destroyed. Try doing
process.destroy()
when you're done using it.