Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Async task to pause program multiple times
    primarykey
    data
    text
    <p>I know the title is somewhat confusing, my problem is this: I want to "pause" my program multiple times (via a loop) using sleep(ms) and make changes to several buttons that the user has pressed. I understand that changes to the UI can only be made in the main thread, so I am wondering how to convert this code into an Async task. I also remember reading somewhere that Async task can only be called once? If this is so, what other solutions could I look into?</p> <pre><code>View.OnClickListener SimulateButtonHandler = new View.OnClickListener() { @Override public void onClick(View v) { for(int j=0;j&lt;169;j++) //169 buttons to click { if(ClickedPoints[j]!=null) //how I keep track of which buttons has clicked { //clear button color Drawable d2 = ClickedPoints[j].getBackground(); ClickedPoints[j].invalidateDrawable(d2); d2.clearColorFilter(); } } Timer timer = new Timer(); for(int i=0;i&lt;169;i++) { if(ClickedPoints[i]!=null) { //change the color after some time Simulate(timer, i); //clear color again Drawable d2 = ClickedPoints[i].getBackground(); ClickedPoints[i].invalidateDrawable(d2); d2.clearColorFilter(); } } } }; public void Simulate(Timer timer, final int index) { timer.schedule(new TimerTask() { public void run() { Sim(index); } }, 1000); //delay for some amount of time, then rehighlight the button } private void Sim(int i) { Drawable d1 = ClickedPoints[i].getBackground(); PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP); d1.setColorFilter(filter); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload