3

私は多くの問題を経験してきましたが、タイマーを一時停止および一時停止解除しようとしました。向きを縦向きまたは横向きにロックすると機能しますが、それは私がやりたいことではありません。もちろん、向きを変更するとonCreateメソッドが呼び出されるので、timertaskをキャンセルしてnullに設定しますが、向きを複数回実行した後は、timertaskはキャンセルされなくなります。私はここで他の人々の質問を調べましたが、私の質問に対する答えを持っている人はいないようです。これが私のコードです。iveがそれを機能させるために私ができるすべてのことを試みているので、現時点では少しずさんです。

public class singleTimer extends Activity implements OnClickListener {

private Integer setTime = 0;
private Integer tmrSeconds = 0;
private Integer tmrMilliSeconds = 0;
private Timer myTimer = new Timer();
private TimerTask myTimerTask;
private TextView timerText;
private boolean isPaused = true;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_timer);
    Bundle extras = getIntent().getExtras();
    setTime = extras.getInt("com.bv.armyprt.timer_duration");
    if (myTimerTask != null) {
        myTimerTask.cancel();
        myTimerTask = null;
    }
    if (savedInstanceState != null) {
        if (savedInstanceState.getInt("tmrSeconds") == 0) {
            tmrSeconds = setTime;
        } else {
            tmrSeconds = savedInstanceState.getInt("tmrSeconds");
            tmrMilliSeconds = savedInstanceState.getInt("tmrMilliseconds");

            if (isPaused == false) {
                myTimer = new Timer();
                myTimerTask = new TimerTask() {
                    @Override
                    public void run() {
                        TimerMethod();
                    }
                };
                myTimer.schedule(myTimerTask, 0, 100);
            }

        }
    } else {
        tmrSeconds = setTime;
    }
    timerText = (TextView)findViewById(R.id.timerText);
    timerText.setText(String.format("%03d.%d", tmrSeconds, tmrMilliSeconds));

    TextView timerDesc = (TextView)findViewById(R.id.timerDescription);
    timerDesc.setText("Timer for: " + setTime.toString());
    Button startButton = (Button)findViewById(R.id.timerStart);
    Button stopButton = (Button)findViewById(R.id.timerStop);
    Button closeButton = (Button)findViewById(R.id.timerClose);
    closeButton.setOnClickListener(this);
    startButton.setOnClickListener(this);
    stopButton.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case (R.id.timerStart):
        isPaused = false;
        myTimer = new Timer();
        myTimerTask = new TimerTask() {
            @Override
            public void run() {
                TimerMethod();
            }
        };
        myTimer.schedule(myTimerTask,0, 100);
        break;

    case (R.id.timerStop):
        isPaused = true;
        myTimerTask.cancel();
        myTimerTask = null;
        myTimer.cancel();

        break;

    case (R.id.timerClose):
        onDestroy();
        this.finish();
        break;
    }

}
private void TimerMethod()
{
    //This method is called directly by the timer
    //and runs in the same thread as the timer.
    //We call the method that will work with the UI
    //through the runOnUiThread method.
    this.
    tmrMilliSeconds--;
    this.runOnUiThread(Timer_Tick);
}

private Runnable Timer_Tick = new Runnable() {
    public void run() {

    //This method runs in the same thread as the UI.               
        if (tmrSeconds > 0) {
            if (tmrMilliSeconds <= 0) {
                tmrSeconds--;
                tmrMilliSeconds = 9;
            }
        } else {
            Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
            v.vibrate(1000);
            myTimer.cancel();
            tmrSeconds = setTime;
            tmrMilliSeconds = 0;
            isPaused = true;
        }

    //Do something to the UI thread here
        timerText.setText(String.format("%03d.%d", tmrSeconds, tmrMilliSeconds));
    }
};

@Override
public void onSaveInstanceState(Bundle savedInstanceState){
    savedInstanceState.putInt("setTimer", setTime);
    savedInstanceState.putInt("tmrSeconds", tmrSeconds);
    savedInstanceState.putInt("tmrMilliseconds", tmrMilliSeconds);

    super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    setTime = savedInstanceState.getInt("setTimer");
    tmrSeconds = savedInstanceState.getInt("tmrSeconds");
    tmrMilliSeconds = savedInstanceState.getInt("tmrMilliSeconds");
}
}
4

2 に答える 2

11

ブール変数を追加するだけです

boolean stopTImer = false ;

そして、timerTaskで、次のようなことを行います。

@Overrride
public void run(){
if(!stopTimer){
//do stuff ...
//...
}

停止したい場合は、ブール値をtrue

于 2011-05-27T22:59:38.157 に答える
2

onStopの間はタイマーを停止する必要があります。Androidはアクティビティの別のインスタンスを作成する可能性があり、向きを変更すると前のタイマー(タスク)への参照が失われます。

アクティビティに関連付けられているすべてのオブジェクトは、アクティビティのライフサイクルに従います。つまり、アクティビティが削除されてもオブジェクトを保持したい場合は、オブジェクトへの参照を別の場所に保存する必要があります(これは非常に頻繁に発生する可能性があります)。

于 2011-05-27T22:53:19.637 に答える