2014-03-25 7 views
0

버튼을 만들었습니다. 버튼을 클릭하면 스레드가 잠자기 야합니다. 이제 버튼을 클릭하면 programm가 잠자 지 않는 문제가 있습니다. 추가 작업을 통해 테스트 한 버튼의 작업은 다른 배경을 설정합니다.스레드를 멈추게하는 버튼

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; 
     } 

    } 
+1

이 어디 스레드 자고 만들 수를 만들 수있는 올바른 방법은 무엇입니까? 당신은 UI 스레드에서 잠을 자고 있습니까 ?? – Raghunandan

+0

메소드 crunch가 다른 클래스에 의해 호출됩니다. 스레드에서 전체 메서드를 설정해야합니까? – basti12354

+0

매우 혼란 스럽습니다. 다른 클래스. 그것은 활동 클래스인가? 어떤 스레드가 당신에 대해 – Raghunandan

답변

0

이 스레드

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

스레드가

timer.sleep(1000);