Android-如何停止和暂停计时器
我已经处理了很多问题,试图暂停和取消暂停计时器,如果我将方向锁定为纵向或横向,它会工作,但这并不是我想要做的。当然,更改方向时会调用onCreate方法,因此我会取消timertask并将其设置为null,但在多次运行方向后,它不再取消timertask。我在这里查阅了其他人的问题,但似乎没有人能回答我的问题。这是我的密码。现在有点草率,因为我一直在想尽一切办法让它工作Android-如何停止和暂停计时器,android,timer,timertask,Android,Timer,Timertask,我已经处理了很多问题,试图暂停和取消暂停计时器,如果我将方向锁定为纵向或横向,它会工作,但这并不是我想要做的。当然,更改方向时会调用onCreate方法,因此我会取消timertask并将其设置为null,但在多次运行方向后,它不再取消timertask。我在这里查阅了其他人的问题,但似乎没有人能回答我的问题。这是我的密码。现在有点草率,因为我一直在想尽一切办法让它工作 public class singleTimer extends Activity implements OnClickLis
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");
}
}
公共类singleTimer扩展活动实现OnClickListener{
私有整数setTime=0;
私有整数tmrSeconds=0;
私有整数tmrMilliSeconds=0;
专用计时器myTimer=新计时器();
私人TimerTask myTimerTask;
私有文本查看时间文本;
私有布尔值isPaused=true;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.single_timer);
Bundle extras=getIntent().getExtras();
setTime=extras.getInt(“com.bv.armyprt.timer_duration”);
如果(myTimerTask!=null){
myTimerTask.cancel();
myTimerTask=null;
}
如果(savedInstanceState!=null){
如果(savedInstanceState.getInt(“tmrSeconds”)==0){
tmrSeconds=设置时间;
}否则{
tmrSeconds=savedInstanceState.getInt(“tmrSeconds”);
tmrMilliSeconds=savedInstanceState.getInt(“tmrMilliSeconds”);
如果(isPaused==false){
myTimer=新计时器();
myTimerTask=新TimerTask(){
@凌驾
公开募捐{
TimerMethod();
}
};
myTimer.schedule(myTimerTask,01100);
}
}
}否则{
tmrSeconds=设置时间;
}
timerText=(TextView)findViewById(R.id.timerText);
timerText.setText(String.format(“%03d.%d”,tmrSeconds,tmrMilliSeconds));
TextView timerDesc=(TextView)findViewById(R.id.timerDescription);
timerDesc.setText(“计时器:”+setTime.toString());
按钮开始按钮=(按钮)findViewById(R.id.timerStart);
按钮停止按钮=(按钮)findViewById(R.id.timerStop);
按钮关闭按钮=(按钮)findViewById(R.id.timerClose);
closeButton.setOnClickListener(此);
setOnClickListener(这个);
stopButton.setOnClickListener(此);
}
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
开关(v.getId()){
案例(R.id.timerStart):
isPaused=false;
myTimer=新计时器();
myTimerTask=新TimerTask(){
@凌驾
公开募捐{
TimerMethod();
}
};
myTimer.schedule(myTimerTask,01100);
打破
案例(R.id.timerStop):
isPaused=真;
myTimerTask.cancel();
myTimerTask=null;
myTimer.cancel();
打破
案例(R.id.timerClose):
onDestroy();
这个;
打破
}
}
私有void TimerMethod()
{
//此方法由计时器直接调用
//并与计时器在同一线程中运行。
//我们调用将与UI一起工作的方法
//通过runOnUiThread方法。
这
tmrMilliSeconds--;
此.runOnUiThread(计时器滴答声);
}
私有可运行计时器\u Tick=new Runnable(){
公开募捐{
//此方法与UI在同一线程中运行。
如果(TMR秒>0){
如果(tmrMilliSeconds您应该在启动时停止计时器。Android可能会创建您活动的另一个实例,当您更改方向时,您将丢失对上一个计时器(任务)的引用
与活动关联的所有对象都遵循活动生命周期。这意味着您必须将对对象的引用存储在其他位置,即使活动被删除(这种情况经常发生)。您只需添加一个布尔变量即可
boolean stopTImer = false ;
在timerTask中,执行以下操作:
@Overrride
public void run(){
if(!stopTimer){
//do stuff ...
//...
}
当您想要停止它时,将布尔值设置为true
“当然,更改方向时会调用onCreate方法”-你意识到它比那稍微复杂一点吗?当方向改变时,活动被完全破坏,然后重新创建。这对我尝试做的部分工作有效,但我需要能够停止并恢复计时器。这就是为什么你应该将计时器引用存储在更全局的位置。通过你的实现,你可以如果活动被删除并重新实例化,则会丢失计时器引用。因为您暂停了计时器(或设置了暂停计时器)在onCreate期间,您不会暂停原始计时器,而是暂停新的计时器。您至少应该在onStop期间设置暂停标志,以防止原始计时器流氓。这就是我最后要做的,我使用了isPaused boolean,它会在run方法上检查它。感谢帮助