android开发教程之间隔执行程序(android计时器)
时间:2023-11-29 22:43:51
下面是每隔一段时间就执行某个操作,直到关闭定时操作:
final Handler handler = new Handler();
Runnable runnable = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
// 在此处添加执行的代码
handler.postDelayed(this, 50);// 50是延时时长
}
};
handler.postDelayed(runnable, 50);// 打开定时器,执行操作
handler.removeCallbacks(this);// 关闭定时器处理
下面是隔一段时间后执行某个操作一次,执行完后,不再执行
final Handler handler = new Handler();
runCount = 0;// 全局变量,用于判断是否是第一次执行
Runnable runnable = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
if(runCount == 1){// 第一次执行则关闭定时执行操作
// 在此处添加执行的代码
handler.removeCallbacks(this);
}
handler.postDelayed(this, 50);
runCount++;
}
};
handler.postDelayed(runnable, 50);// 打开定时器,执行操作
标签:android,计时器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android使用IntentService进行apk更新示例代码
2022-01-19 12:12:07
![](https://img.aspxhome.com/file/2023/4/99804_0s.gif)
Spring中的注解之@Override和@Autowired
2022-08-07 19:56:47
![](https://img.aspxhome.com/file/2023/4/125564_0s.png)
Android开发实现浏览器全屏显示功能
2022-01-01 18:01:48
java中List接口与实现类介绍
2022-11-17 02:54:40
Android客户端与服务端数据加密传输方案详解
2023-07-14 13:55:37
![](https://img.aspxhome.com/file/2023/7/82397_0s.jpg)
C#实现给Word每一页设置不同文字水印的方法详解
2023-07-01 18:49:40
![](https://img.aspxhome.com/file/2023/6/111276_0s.png)
Java Swing JPanel面板的使用方法
2023-02-13 12:04:49
![](https://img.aspxhome.com/file/2023/2/80892_0s.png)
解析Android框架之Volley源码
2021-09-30 19:44:28
![](https://img.aspxhome.com/file/2023/9/109179_0s.png)
Java中的SuppressWarnings注解使用
2023-08-18 17:31:19
![](https://img.aspxhome.com/file/2023/9/78269_0s.png)
深入浅析Java Object Serialization与 Hadoop 序列化
2023-07-30 17:02:16
Android自定义 WebView浏览器
2023-07-21 00:31:34
Android仿iOS侧滑退出当前界面功能
2022-07-01 10:08:37
![](https://img.aspxhome.com/file/2023/0/120070_0s.gif)
MybatisPlus实现对象嵌套关联查询一对多List集合查询
2022-04-27 16:00:12
![](https://img.aspxhome.com/file/2023/3/116173_0s.png)
使用 Lambda 取代 Android 中的匿名类
2023-11-16 14:01:04
![](https://img.aspxhome.com/file/2023/6/106616_0s.jpg)
Java SpringBoot详解集成以及配置Swagger流程
2023-10-06 03:10:12
![](https://img.aspxhome.com/file/2023/1/122251_0s.png)
Android RecyclerView线性布局详解(1)
2021-09-16 07:20:25
![](https://img.aspxhome.com/file/2023/4/113274_0s.png)
C++中的异常处理机制详解
2023-04-16 16:01:10
Android开发技巧之在a标签或TextView控件中单击链接弹出Activity(自定义动作)
2023-12-26 09:11:39
![](https://img.aspxhome.com/file/2023/0/139440_0s.png)
解决mybatis 中collection嵌套collection引发的bug
2023-03-20 20:55:39
保证缓存和数据库的数据一致性详解
2023-11-18 08:10:44
![](https://img.aspxhome.com/file/2023/3/59573_0s.png)