1

i am trying to make kind of player, so, I setup play button and pause button and make them clickListener . when i press play button the thread start (and it work fine) but the pause button dont work until the thread is finished. what can i do to fix it?

constructor:

 play = false;
 playButton=(ImageButton)findViewById(R.id.imageButton1);
 playButton.setOnClickListener(playButtonClick);

the runnable:

private Runnable runnable=new Runnable() {      
@Override
public void run() {
    int count=0;
    long time=System.currentTimeMillis();
    while(count<24*3 && play)
    {
        if (System.currentTimeMillis()-time>41.67){
            count++;
            time=System.currentTimeMillis();
            scene.play();
            draw(sf.getHolder());
        }
    }
    play=false;
}

clickListener:

 private OnClickListener playButtonClick=new OnClickListener() {        
    @Override
    public void onClick(View v) {
               if (!play){
                      play = true           
                      scene=new TestScene(3,t);
                      thread=new Thread(runnable);
                      thread.run();
                }else
                      play = false;
    }
};

the problem: when the thread is run i cant stop it the function clicklistner is working just when the thread is stop.

tzahibs
  • 45
  • 1
  • 7

2 Answers2

0

Based on your question, A Thread is running in it's own space. The only way to halt a Thread is to tell it to Halt. You could set some static variable that the Thread is listening on. The Thread can take necessary action depending on the state of the variable. In the simplest scenario Pressing the pause button could change a static variable in the Thread class file

Erik
  • 4,514
  • 10
  • 55
  • 112
  • i tried as you can see in the code above. the problem is when the thread is run the click listener not work – tzahibs Mar 13 '14 at 23:25
  • Use a Handler or read [this](http://stackoverflow.com/questions/17897614/android-backgroud-thread-cannot-change-textview-through-handler) – Erik Mar 14 '14 at 14:32
0

Try using Thread.start() instead of Thread.run()

See Thread Docs:

If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.

Edit: You can also read this answer for more information about start vs. run

Community
  • 1
  • 1
Tsikon
  • 304
  • 1
  • 3
  • 13