android如何暂停倒计时,Android – 如何停止和暂停计时器

我一直在经历很多问题尝试暂停和取消暂停计时器,如果我将方向锁定为纵向或横向它可以工作,但这不完全是我想要做的.当然,当您更改方向时会调用onCreate方法,因此我取消了我的timertask并将其设置为null,但是在多次运行方向后,它不再取消时间任务.我在这里查看了其他人的问题,但似乎没有人能够回答我的问题.继承我的代码.它现在有点草率,因为我一直在尝试我能做的一切.

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");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值