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
naresh
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
N.Droid
source
to share
I would suggest you take a look at AsyncTask and IntentService .
+1
dbm
source
to share
// 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
Ravi1187342
source
to share