Thread -while Loop- [Android]
So in my further attempts to implement a while loop in android I have come up with the code below : private boolean connected = false; private Thread loop = new Thread() { publ
Solution 1:
Did you tried AsyncTask ? What you can do...start a new AsyncTask on firstButton click and cancel it on secondButton click.
//define global variable
private DoSomething doSomething;
//register firstButton onClickListener
firstButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//start your asynctask
if(doSomething == null || doSomething.isCancelled()){
doSomething = new DoSomething();
doSomething = doSomething.execute();
}
}
});
//register secondButton onClickListener
secondButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
doSomething.cancel();
}
});
//Inner AsyncTask class
class DoSomething extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
//doSomething();
while(true){
System.out.println(1);
if(isCancelled()){
break;
}
}
return null;
}
}
Note: This is pseudocode...might contain error...just want to give you overview. hope this help.
Post a Comment for "Thread -while Loop- [Android]"