Dimitri
Dimitri

Reputation: 687

Stopping public void run() Android

i need to stop the run() thread after 30sec or anytime i clicked on a button. My question is how can i stop public void run().

   @Override
        public void run() {
            // TODO Auto-generated method stub
              int currentPosition= 0;
                int total = mp.getDuration();
                while (mp!=null && currentPosition<total) {
                    try {

                        Thread.sleep(1000);

                        currentPosition= mp.getCurrentPosition();
                    } catch (InterruptedException e) {
                        return;
                    } catch (Exception e) {
                        return;
                    }        

                    sbMusicProgress.setProgress(currentPosition);

                    /*MP3 PROGRESS*/
                    timer_count++;

                    runOnUiThread(new Runnable() {
                        public void run() {


                            if (timer_count<10)
                                context.txMp3Prog.setText("00:0"+String.valueOf(timer_count));
                            //Stop playlist after 30seconds

                            else if (timer_count==30){
                                timer_count=0;
                                context.txMp3Prog.setText("00:00");
                                mp.pause();
                                sbMusicProgress.setProgress(0);
                                btPlayMp3.setBackgroundResource(R.drawable.air_deezer_play);                        
                            }

                            else
                                context.txMp3Prog.setText("00:"+String.valueOf(timer_count));

                            }

                        });


                }
        }

Upvotes: 0

Views: 1938

Answers (2)

Gru
Gru

Reputation: 2694

First thing that I wanted to mentioned is stop() method of thread is deprecated.

So what is the good practice to stop a thread?

Ans. is you have to complete the life-cycle of thread by completing run() method of of particular Thread.

In your case try to complete the run() method by setting one flag variable after 30 sec or on click of button.

And second solution which mentioned by @Raghunandan.

Try this link which is oracle doc they had explain about thread interrupts.

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

You can call interrupt on your thread.

http://developer.android.com/reference/java/lang/Thread.html

public void interrupt ()

Posts an interrupt request to this Thread. The behavior depends on the state of this Thread:

  1. Threads blocked in one of Object's wait() methods or one of Thread's join() or sleep() methods will be woken up, their interrupt status will be cleared, and they receive an InterruptedException.

  2. Threads blocked in an I/O operation of an InterruptibleChannel will have their interrupt status set and receive an ClosedByInterruptException. Also, the channel will be closed.

  3. Threads blocked in a Selector will have their interrupt status set and return immediately. They don't receive an exception in this case.

I would suggest you to use a Handler.

 int count =30;
 Handler m_handler;
 Runnable m_handlerTask ;
 m_handlerTask = new Runnable()
 {
     @Override 
     public void run() { 
   if(count>=0)
   {     
         // do something 
     count--;    
   }
   else
   {
    m_handler.removeCallbacks(m_handlerTask); // cancel the run                     
   } 
  m_handler.postDelayed(m_handlerTask, 1000);    
  }
  };
  m_handlerTask.run(); 

public final void removeCallbacks (Runnable r)

Remove any pending posts of Runnable r that are in the message queue.

Upvotes: 1

Related Questions