0

I am developing a quiz application in which I am using runnable thread for seekbar functionality. seekbar shows how much time is remaining. Now I want to stop the seekbar when a answer button is click.

here is my code for runnable thread.

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

             while (seekbarStatus < 100) {
                  seekbarStatus = LoadingStatus();
                  //  sleep 1 second to show the progress
                  try {
                          Thread.sleep(1000);
                      } 
                  catch (InterruptedException e) {
                          e.printStackTrace();
                      }

                  // Update the progress bar
                  progressBarHandler.post(new Runnable() {
                      public void run() {
                          seekbar.setProgress(seekbarStatus);
                      }
                  });
             }
             if (seekbarStatus >= 100) {

                 // sleep for  2 seconds, so that you can see the 100% of file download
                 runOnUiThread(new Runnable() {
                     @Override
                     public void run() {

                         if(!(Ans_1.getTag()=="clicked" ||Ans_2.getTag()=="clicked" || Ans_3.getTag()=="clicked" ))
                         {
                             Ans_1.setEnabled(false);
                             Ans_2.setEnabled(false);
                             Ans_3.setEnabled(false);
                             Ans_1.setBackgroundColor(Color.GREEN);
                             points.setText("0 Punkt");
                             new Handler().postDelayed(new Runnable(){
                                    @Override
                                    public void run() {
                                        /* Create an Intent that will start the Menu-Activity. */
                                        showDialogView();

                                    }
                                }, SPLASH_DISPLAY_LENGHT);

                         }


                    }

                    public void showDialogView() {
                        // TODO Auto-generated method stub
                        dialog.show();
                    }
                });

             }
         }


        }).start();

please help me. Any help would be appreciated.

I am not getting how to solve it.

Thanks in advance.

suraj shukla
  • 258
  • 1
  • 3
  • 14
  • possible duplicate of [removeCallbacks not stopping runnable](http://stackoverflow.com/questions/5844308/removecallbacks-not-stopping-runnable) – BlackHatSamurai May 26 '14 at 16:02

1 Answers1

2

Just add an instance variable:

private boolean isCancelled = false;

Then in the button onClick:

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        isCancelled = true;

    }
});

Then change

while (seekbarStatus < 100) {

to

while (seekbarStatus < 100 && !isCancelled) {
user184994
  • 17,791
  • 1
  • 46
  • 52