如何在一定时间后停止计时器?

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

我有一个Android应用程序,它有一个运行任务的计时器:

I have an android application which has a timer to run a task:

time2.scheduleAtFixedRate(new TimerTask() { @Override public void run() { sendSamples(); } }, sampling_interval, sending_interval);

让我们说sampling_interval是2000而sending_interval是4000.

Lets say sampling_interval is 2000 and sending_interval is 4000.

所以在这个应用程序中,我将一些读数值从传感器发送到服务器。但我想在10000(10秒)后停止发送。

So in this application I send some reading values from a sensor to the server. But I want to stop the sending after 10000 (10 seconds).

我该怎么办?

推荐答案

尝试

time2.scheduleAtFixedRate(new TimerTask() { long t0 = System.currentTimeMillis(); @Override public void run() { if (System.currentTimeMillis() - t0 > 10 * 1000) { cancel(); } else { sendSamples(); } } ...

更多推荐

如何在一定时间后停止计时器?

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

发布评论

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

>www.elefans.com

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