Android – 如何停止和暂停计时器

Android – 如何停止和暂停计时器,第1张

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

@H_502_11@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;@OverrIDepublic 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,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);}@OverrIDepublic 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,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",tmrMilliSeconds)); }};@OverrIDepublic voID onSaveInstanceState(Bundle savedInstanceState){ savedInstanceState.putInt("setTimer",setTime); savedInstanceState.putInt("tmrSeconds",tmrSeconds); savedInstanceState.putInt("tmrMilliseconds",tmrMilliSeconds); super.onSaveInstanceState(savedInstanceState);}@OverrIDepublic voID onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); setTime = savedInstanceState.getInt("setTimer"); tmrSeconds = savedInstanceState.getInt("tmrSeconds"); tmrMilliSeconds = savedInstanceState.getInt("tmrMilliSeconds");}}解决方法 你可以简单地添加一个布尔变量

@H_502_11@boolean stopTImer = false ;

并在您的timerTask中,执行以下 *** 作:

@H_502_11@@OverrrIDepublic voID run(){if(!stopTimer){//do stuff ...//...}

当你想要停止它时,将布尔值设置为true

总结

以上是内存溢出为你收集整理的Android – 如何停止和暂停计时器全部内容,希望文章能够帮你解决Android – 如何停止和暂停计时器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1123045.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存