0

I created a button, now I want to add, if I click it the thread should sleep. Now I have the problem that the programm isn't sleeping, if I click the button. The work of the button I test by adding set another background.

ToggleButton t;
LinearLayout l;
boolean pausegedrückt;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    tts = new TextToSpeech(this, this);
    pausegedrückt=false;

public void Crunch(int anzahl) {


         setContentView(R.layout.crunch); 

        //Start und Stop Button
            t=(ToggleButton) findViewById(R.id.toggleButton1);
            t.setOnCheckedChangeListener(this);
            l=(LinearLayout)findViewById(R.id.layout);

        while (pausegedrückt) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }            
         tts.speak("Übung Grandsch: In 10 Sekunden geht`s los! Mach dich bereit!", TextToSpeech.QUEUE_ADD, null);
         tts.playSilence(9000, TextToSpeech.QUEUE_ADD, null);
         tts.speak("Los gehts!", TextToSpeech.QUEUE_ADD, null);
         tts.speak("Mache" + anzahl + "Wiederholungen!" , TextToSpeech.QUEUE_ADD, null);
         for (int i = 1; i < anzahl+1; i++) {
                String str = String.valueOf(i);
                tts.speak(str, TextToSpeech.QUEUE_ADD, null);
                tts.playSilence(3000, TextToSpeech.QUEUE_ADD, null); }
         tts.speak("Übung beendet!", TextToSpeech.QUEUE_ADD, null);


    }

@Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            l.setBackgroundColor(Color.BLUE);
            pausegedrückt=true;
        } else {
            l.setBackgroundColor(Color.BLACK);
            pausegedrückt=false;
        }

    }
canova
  • 3,965
  • 2
  • 22
  • 39
basti12354
  • 2,490
  • 4
  • 24
  • 43
  • 1
    where is the thread? You are calling sleep on the ui thread?? – Raghunandan Mar 25 '14 at 11:46
  • the method crunch is called by another class. Do I've to set the whole method in a thread? – basti12354 Mar 25 '14 at 11:50
  • very confusing. another class. is it activity class?. what thread are you talking about – Raghunandan Mar 25 '14 at 11:51
  • the other class is the activity-class, which get inherited from the class of my post. – basti12354 Mar 25 '14 at 11:54
  • http://stackoverflow.com/questions/19894607/java-how-to-stop-thread –  Mar 25 '14 at 12:00
  • possible duplicate of [How to properly stop the Thread in Java](http://stackoverflow.com/questions/10961714/how-to-properly-stop-the-thread-in-java) –  Mar 25 '14 at 12:00
  • Vote to close, there are enormous amount of questions about that, which are properly resolved. –  Mar 25 '14 at 12:01
  • Please read Use of [Processes and Threads](http://developer.android.com/guide/components/processes-and-threads.html) in Android. Check [painless-threading](http://android-developers.blogspot.in/2009/05/painless-threading.html) also. – Aduait Pokhriyal Mar 25 '14 at 12:09

1 Answers1

0

this is the right way to create a thread

    Thread timer = new Thread() {
        @Override
        public void run() {
            try {
                //try something here
            } catch (InterruptedException e) {
                e.printStackTrace();
        }
    };
    timer.start();

to make your thread sleeps

timer.sleep(1000);
wael
  • 444
  • 4
  • 11