2012-03-23 10 views
1

このスレッドは、ユーザがbuttonStartを押すとゆっくりとループし、integerTimeの間は一時停止する必要があります。私はAndroidになると素人ですが、文字通りこれを動作させるために約12時間を費やしました。誰かが私のコードを助けたり訂正したりすることができれば非常に感謝しています。乾杯。なぜこのスレッドは一時停止してループしませんか?

final Button button = (Button) findViewById(R.id.buttonStart);    // Making the button to launch the 
    button.setOnClickListener(new View.OnClickListener() {      // actions below 
     public void onClick(View v) { 

      editTextTarget = (EditText)findViewById(R.id.editTextTarget);  // After user presses 'Start', it will 
      stringTarget = editTextTarget.getText().toString();    // convert the input 'Target' into a string 

      editTextPayload = (EditText)findViewById(R.id.editTextPayload); // After user presses 'Start', it will 
      stringPayload = editTextPayload.getText().toString();    // convert the input 'Payload' into a string and 
      integerPayload = Integer.parseInt(stringPayload);    // then convert the string into an integer 

      editTextTime =(EditText)findViewById(R.id.editTextTime);  // After user presses 'Start', it will 
      stringTime = editTextTime.getText().toString();    // convert the input 'Time' into a string and 
      integerTime = Integer.parseInt(stringTime);     // then convert the string into an integer 

      Thread threadBegin = new Thread() 
      { 
      public void run(){ 
       while (DOSrunning.get()) { 
       try { 
        Thread.sleep(integerTime);    
       } catch (ClientProtocolException e) { 
       } catch (IOException e) { 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       } 
        } 
      ;}; 

      threadBegin.start(); 
+0

ミリ秒単位の整数ですか? –

+0

あなたの要件は不明です... – waqaslam

答えて

1

あなたのコードは私にはうまく見えます。 run()メソッドをオーバーライドして、Threadに拡張された匿名クラスを作成しています。 run()の前に@Overrideを持っている必要があります。内側ループの一部を切り取っていない限り、次のキャッチが必要な理由が混乱します。

} catch (ClientProtocolException e) { 
} catch (IOException e) { 

私もDOSrunning()が設定されますどのように表示されません。それは良いですAtomicBooleanのように見えます。 trueに初期化されていますか?

AtomicBoolean DOSrunning = new AtomicBoolean(true); 

そうでない場合は、その問題はintergerTimeの値はミリ秒単位ではないことでなければなりません。値が1000の場合、1秒間スリープ状態になります。

最後に、必ずInterruptedExceptionを正しく処理してください。このようなものがより良いパターンです:

} catch (InterruptedException e) { 
    // reset the interrupted flag 
    Thread.currentThread().interrupt(); 
    e.printStackTrace(); 
    // quit the thread if necessary 
    return; 
} 
関連する問題