0

I've an andoid (API 21) mainactivity in which a start a thread. Pressing a button i close the thread and mainactivity. Is this a good solution to interrupt thread and close activity? I try to call

mythread.cancel()
finishAndRemoveTask();

But when mainactivity/programs closes it crashes because the thread is still alive.

So when button is clicked i try to change the code and write :

    mythread.interrupt();
    finishAndRemoveTask();

Is this a good solution ? (i don't know if i can use mythread.join() because the thread runs infinitely..

In OnCreate i create and start the thread

     myThread_read = new thread_read(handler_read);
     //starting thread
     myThread_read.start();

I know i must use Runnable but this code extends Thread class. The thread code is:

   private class thread_read extends Thread {
        Handler mHandler;
        String logString ;

        protected volatile boolean running;

        /* constructor */
        thread_read(Handler h) {
            mHandler = h;
        }

        public void start()
        {
            Logger.WriteString("thread_read", "Starting");
            running = true;
            super.start();

        }

        public void cancel()
        {
            Logger.WriteString("thread_read", "Stopping");
            running = false;

        }


        public  void run() {


            while (running) {

                try {
                    Thread.sleep(500);  // 500 msecs
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }

                // do works
                }

            }
        }
    }
Gab74
  • 45
  • 5

3 Answers3

0

You can try hosting the thread from the asynctask and then try to stop the activity. If you need to stop the thread once after the job has been completed do it on the postExecute of asynctask.

Also can another method by try stopping the thread by interrupt() in the destroy on the activity. You can modify the run method as

while(interrupted()){return;}

Can refer the following link How to stop a running thread when Activity on destroy at Android?

Community
  • 1
  • 1
PranavKAndro
  • 921
  • 7
  • 9
0

You can try hosting the thread from the asynctask and then try to stop the activity. If you need to stop the thread once after the job has been completed do it on the postExecute of asynctask.

Also can another method by try stopping the thread by interrupt() in the destroy on the activity. You can modify the run method as

while(interrupted()){return;}

joshua787
  • 11
  • 6
  • You mean that a can remove mythread.interrupt(); and insert mythread.cancel() in OnDestroy method ?? @Override public void onDestroy() { mythread.cancel(); super.onDestroy(); } – Gab74 Apr 19 '17 at 09:34
0

Yes Gab74.. Add the mythread.interupt in the ondestroy by extending the onDestroy()

and modify the run method of the thread with While(interrupted()){return;} That is write a logic if interrupted then exit the thread.

PranavKAndro
  • 921
  • 7
  • 9