如何停止计时器的Andr​​oid

编程入门 行业动态 更新时间:2024-10-28 08:18:51
本文介绍了如何停止计时器的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下是我的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_alert 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; } } };

更多推荐

如何停止计时器的Andr​​oid

本文发布于:2023-11-06 17:07:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564285.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计时器   Andr   oid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!