如何停止计时器的Android [英] How to Stop Timer Android
问题描述
以下是我的code:
在MainActivity.class
In MainActivity.class
private OnCheckedChangeListener alert_on_off_listener = new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup groupname, int CheckedButtonId) {
if(CheckedButtonId==R.id.radiobutton_on){
Toast.makeText(getApplicationContext(), "radio on", Toast.LENGTH_LONG).show();
alert_on = true;
display_alert();
}
else{
alert_on = false;
}
}
};
public void display_alert(){
int delay = 10000;
Timer timer =new Timer();
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
Intent myIntent = new Intent(HoraWatchActivity.this,MyService.class);
startService(myIntent);
}
}, delay,10000);
}
MyService.class
MyService.class
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if(MainActivity.alert_on)
Toast.makeText(getApplicationContext(), "Alert is On", Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "Alert is Off",Toast.LENGTH_LONG).show();
}
我检查 onCheckedChangeListener
并调用 display_alert
根据检查单选按钮的功能。
I am checking the onCheckedChangeListener
and calling display_alert
function according to the checked radio button.
在 display_alert
我是用定时器调度以10秒的固定利率,并要求为MyService
类的功能。
In display_alert
function I was using timer scheduling at fixed rate of 10 seconds and calling myservice
class.
所以,当我检查了 radio_on
按钮,它会调用我的 display_aler
T功能,并得到了警惕的在致祝酒辞。因此,它做工精细。
So once I checked the radio_on
button, it calls my display_aler
t function and got the "alert on" in toast. So its working fine.
如果我再次检查 radio_off
按钮,然后选中 radio_on
按钮,我收到了警惕的敬酒,但敬酒来了两次。同样的,如果我再次选择 radio_on
按钮,面包显示第三次,但我只需要一次。这是由于该定时器正在运行,每10秒。我想停止计时,一旦我点击 radio_off
按钮。
If I again check radio_off
button and then check the radio_on
button, I am getting the "alert on" toast, but the toast is coming twice. Similarly if I again select the radio_on
button, the toast is displaying for the third time, but I need only once. This was due to that the timer is running for every 10 seconds. I want to stop the timer once I click the radio_off
button.
问题是,一旦我开始计时,我没有再阻止它。当我应如何取消我的课定时任务?
The problem is, once I started the timer, I was not stopping it further. When and how shall I cancel the timer tasks in my class?
推荐答案
首先,在一个独立的方式创建您的TimerTask:
First, create your TimerTask on a separate way:
class MyTimerTask extends TimerTask {
public void run() {
Intent myIntent = new Intent(HoraWatchActivity.this,MyService.class);
startService(myIntent);
}
}
声明它的地方,当然看得见你OnCheckedChangeListener:
Declare it somewhere, of course visible to your OnCheckedChangeListener:
MyTimerTask myTimerTask;
安排的:
public void display_alert() {
int delay = 10000;
myTimerTask = new MyTimerTask();
Timer timer = new Timer();
timer.scheduleAtFixedRate(myTimerTask, delay, 10000);
}
然后修改code为:
And then modify your code to:
private OnCheckedChangeListener alert_on_off_listener = new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup groupname, int CheckedButtonId) {
if(CheckedButtonId==R.id.radiobutton_on){
Toast.makeText(getApplicationContext(), "radio on", Toast.LENGTH_LONG).show();
alert_on = true;
display_alert();
}
else{
myTimerTask.cancel();
alert_on = false;
}
}
};
这篇关于如何停止计时器的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!