2017-11-15 14 views
0

特定の条件が存在するときにカウントダウンタイマーを一時停止するアプリを作成しています。私のカウントダウンタイマーは、テキストフィールドに入力する文字列配列を循環します。カウンタがゼロになるたびに、リセットされ、テキストフィールドに次のテキストセットが設定されます。私は作業中の一時停止ボタンも持っています。しかし、テキストフィールドの特定のテキストに基づいてカウントダウンタイマーをプログラム的に一時停止することはできません。条件に基づいて一時停止をプログラムできません

HERESに私のタイマーコード:

class MyTimer extends CountDownTimer { 

    //constructor for timer class 
    public MyTimer(long millisInFurture, long countDownInterval) { 
     super(millisInFurture, countDownInterval); 
    } 

    // this method called when timer is finished 
    @Override 
    public void onFinish() { 
     // reset all variables 
     clockText.setText(clockTime); 
     isRunning = false; 
     remainMilli = 0; 

     advanceLevel(); 
     goTimer(); 
    } 

    // this method is called for every iteration of time interval 
    @Override 
    public void onTick(long millisUntilFinished) { 
     remainMilli = millisUntilFinished; 

     //calculate minutes and seconds from milliseconds 
     String minute = "" + (millisUntilFinished/1000)/60; 
     String second = "" + (millisUntilFinished/1000)%60; 

     // apply style to minute and second 
     if((millisUntilFinished/1000)/60 < 10) { 
      minute = "0" + (millisUntilFinished/1000)/60; 
     } 
     if ((millisUntilFinished/1000)%60 < 10) { 
      second = "0" + (millisUntilFinished/1000)%60; 
     } 

     //update textview with remaining time 
     clockText.setText(minute + ":" + second); 
    } 

} 

は、ここに私のスタートだとタイマーのコードを一時停止:

public boolean goTimer() { 
    breakCheck = blinds.getText().toString(); 
    Log.i(breakCheck, "level"); 

    if (isRunning) { 
     // cancel (Pause) timer when it is running 
     mTimer.cancel(); 
     mTimer = null; 
     isRunning = false; 
    } else { 
     if (remainMilli == 0) { 
      // start timer from initial time 
      mTimer = new MyTimer(blindTime, 1000); 

     } else { 
      //resume timer from where it is paused 
      mTimer = new MyTimer(remainMilli, 1000); 
     } 
     mTimer.start(); 
     isRunning = true; 
     } 

    return true; 

} 

...と、ここでの各ループの後にテキストフィールドを変更するコードですカウントダウンタイマー:

だから、カウントダウンが終了するたびに、テキストフィールドにはn String配列からのewテキスト私が望むのは、ブラインドのテキストフィールドが "BREAK"を表示しているときに、カウントダウンタイマーを一時停止したいのです。だから、

、私が入れ:

String blindCheck = blinds.getText().toString(); 

(blindCheckが== "BREAK"){mTimer.cancel()場合。 }

...私が何をするにしても、カウントダウンタイマーは一時停止せず、そのまま続行します。

タイマーの[開始/一時停止]ボタンは下部のナビゲーションバーにあり、素晴らしい機能を果たします。したがって、私のblindCheck == "BREAK"では、起動/一時停止の方法を実行しようとしましたが、どちらもうまくいきません。

ここで何をすべきかわかりません。どんな助けもありがとう。 ありがとう、 Hendo

答えて

0

この目的でCountDownTimerを使用しないでください。この代わりに、このようなハンドラーを使用する必要があります

final Handler handler = new Handler(); 
    final Runnable runnable = new Runnable() { 
     @Override 
     public void run() { 

      // done your functionality here 


      // if you want to continue, or pause is not active 
      handler.postDelayed(this, 1*1000); 

      // else you will in pause state 

     } 
    }; 


    // to start the hanlder 
    handler.postDelayed(runnable, 1*1000); 

    // to stop or pause the handler 
    handler.removeCallbacks(runnable); 
+0

だから、私は今ここにあるすべてを拭いていますか?なぜなら、ハンドラを使うのは単なる良い方法だからです。私が今持っているものはすべてうまくいくから...特定の出来事の間にそれを一時停止させることを除いて。ありがとう、ヘンド。 –

+0

私はあなたに太鼓道を与えています –

+0

この方法でもうまくいきますが、テキストフィールドにBREAKと表示されても自動的に一時停止することはできません。 –

関連する問題