2
int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.main);

    Button start = (Button) findViewById(R.id.start);

    start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {

                    try {

                        String[] cmd = new String[] { "logcat", "-f",Environment.getExternalStorageDirectory()+"/log.txt", "-v", "time" };
                        Runtime.getRuntime().exec(cmd);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            });

            thread.start();

        }
    });

    Button stop = (Button) findViewById(R.id.stop);

    stop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            try {
                Runtime.getRuntime().exec("logcat -d");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
    });

    Button addLog = (Button) findViewById(R.id.addLog);

    addLog.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Log.e("logTag", Integer.toString(count));
            count ++ ;

        }
    });
}

I want to start saving my app's log using start button and stop it using stop. The addLog button is used to see if more lines are being added to log.txt file. The start button works properly but the problem is it never ends with stop. Even after pressing stop, when I press addLog button and check the log.txt file, I see last lines have been added. What is my fault ?

I need to start the stream, close this activity and take a tour in other activities then come back and shut the logging machine down.

Mohsen
  • 1,620
  • 3
  • 15
  • 22

2 Answers2

3

Yes you can achieve that by saving the Process in a variable that is returned by the Runtime's exec() method and call destroy() on that variable.

public abstract void destroy ()

Terminates this process and closes any associated streams.

Let's record the Process as a global variable;

private Process p;

and when executing it

p = Runtime.getRuntime().exec(cmd);

and this how it's stopped.

void stopProcess() {
     if (p != null) {
         p.destroy();
     } else {
         Log.d(TAG, "stopProcess: p==null");
     }
}
Ahmed Hegazy
  • 11,465
  • 5
  • 35
  • 62
  • Thank you very much. I know it will work but I have another problem which stops me using the method you explained. I need to start the stream, close this activity and take a tour in other activities then come back and shut the logging machine down. can you help me ? @hegazy – Mohsen May 20 '15 at 16:40
  • would you answer please ? – Mohsen May 20 '15 at 16:52
  • sorry, I wasn't available. Do you still have any problems? – Ahmed Hegazy May 20 '15 at 22:39
1

Just use @hegazy method and save reference to process in your Application class. Don't forget to register it in your manifest. Hope you've got my idea.

Something like that:

((App)getApplicationContext).process = Runtime.getRuntime().exec(cmd);

and than take this when you need it:

((App)getApplicationContext).process.exec("logcat -d");
Community
  • 1
  • 1
Divers
  • 9,063
  • 7
  • 41
  • 87
  • Thank you, it worked. Now I have a new problem. If the user closes my application , the entire app process will be destroyed but the logging machine is still online. And then the reference to the `process` object is lost and I can't stop the stream after reopening my activity. can you help me ? – Mohsen May 20 '15 at 18:08
  • It other question. Close this one and start new one. Don't forget to mark my answer as right one – Divers May 20 '15 at 19:01