Android - How to Safely Stop Running Stream

My application has two actions. The first operation (A1) starts the thread. Suppose this activity (A1) is paused. I want to safely stop the treadmill; how to do it?

thank

+3


source to share


3 answers


you can use return statement in Threads startup method like this ...

public void run(){

    if(isDone)
    {     
      return;
    }

}

      



or you can use this ...

if(thread != null)
{
    Thread t1 = thread;
    thread = null;
    t1.interrupt();
}

      

+1


source


I would suggest you take a look at AsyncTask and IntentService .



+1


source


// use the boolean flag in the side trigger method Thread

like below

boolean isActivityPaused = false;

       public void run(){

    // use isActivityPaused boolean flag here to stop your Thread

        while(!isActivityPaused){ 



        }

        }

      

now it's onPause()

methed yourActivity

public void onPause(){

isActivityPaused = true; // set isActivityPaused= true to stop your `Thread`

}

      

0


source







All Articles