1

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 ?

sjain
  • 23,126
  • 28
  • 107
  • 185
  • does the process.getInputStream() return only the current input? Or every event from before? – mthandr Nov 27 '14 at 15:14
  • well how do I know that ? I just know that my output is increasing every time I click the button. – sjain Nov 27 '14 at 15:19
  • there are so many ways, only get the last line when reading the bufferedReader or put an index when printing lines. Anyway, look at Camal answer – mthandr Nov 27 '14 at 15:25

1 Answers1

1

You're getting duplicates because the process is never destroyed. Try doing process.destroy() when you're done using it.

Carnal
  • 21,744
  • 6
  • 60
  • 75
  • Actually I was using `process.destroy()` before thread start and that didn't worked. I placed it in another activity where I am using button. So I changed to `static Process` and that worked. Strange but yes that was the point. – sjain Nov 27 '14 at 15:48