Android中使用定时器的三种方法
作者:Brioal 时间:2022-06-04 00:16:20
本文实例为大家分享了Android中使用定时器的三种方法,供大家参考,具体内容如下
图示:
因为都比较简单,所以就直接贴代码(虑去再次点击停止的操作),有个全局的Handler负责接收消息更新UI
第一种方法:Thread.sleep();方法
Runnable runnable = new Runnable() {
@Override
public void run() {
while (true) {
mHandler.sendEmptyMessage(0);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
new Thread(runnable).start();
}
第二种方法:Handler的postDelay()方法
final Runnable runnable = new Runnable() {
@Override
public void run() {
if (isStart2) {
mHandler.sendEmptyMessage(0);
mHandler.postDelayed(this, 1000);
}
}
};
mHandler.postDelayed(runnable, 1000);
}
第三种:Timer和TimerTask
private Timer timer = new Timer();
private TimerTask timerTask = new TimerTask() {
@Override
public void run() {
mHandler.sendEmptyMessage(0);
}
};
timer.schedule(timerTask, 1000, 1000);
总的来说第三种方法最方便,不易出错,第二种容易忘记添加出发事件.
贴一下完整代码:
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.brioal.timertest.MainActivity">
<TextView
android:id="@+id/main_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="Hello World!" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/main_tv"
android:layout_marginTop="100dp"
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:onClick="Method1"
android:text="方法1:Thread"
android:textAllCaps="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:onClick="Method2"
android:text="方法2:Handler"
android:textAllCaps="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:onClick="Method3"
android:text="方法3:Task"
android:textAllCaps="false" />
</LinearLayout>
</RelativeLayout>
MainActivity
package com.brioal.timertest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private TextView mTv;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//收到消息后显示当前时间
long current = System.currentTimeMillis();
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
String time = dateFormat.format(current);
mTv.setText(time);
}
};
private Timer timer = new Timer();
private TimerTask timerTask = new TimerTask() {
@Override
public void run() {
mHandler.sendEmptyMessage(0);
}
};
private Thread thread1;
private boolean isStart1 = false;
private boolean isStart2 = false;
private boolean isStart3 = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTv = (TextView) findViewById(R.id.main_tv);
}
//Thread方法
public void Method1(View view) {
Runnable runnable = new Runnable() {
@Override
public void run() {
while (isStart1) {
mHandler.sendEmptyMessage(0);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
if (isStart1) {
isStart1 = false;
} else {
isStart1 = true;
thread1 = new Thread(runnable);
thread1.start();
}
}
public void Method2(View view) {
final Runnable runnable = new Runnable() {
@Override
public void run() {
if (isStart2) {
mHandler.sendEmptyMessage(0);
mHandler.postDelayed(this, 1000);
}
}
};
if (isStart2) {
isStart2 = false;
} else {
mHandler.postDelayed(runnable, 1000);
isStart2 = true;
}
}
public void Method3(View view) {
if (isStart3) {
timer.cancel();
isStart3 = false;
} else {
timer.schedule(timerTask, 1000, 1000);
isStart3 = true;
}
}
}
总结完了,完整Github地址:TimerTest
更多内容请点击专题《java定时功能》进行学习。
标签:Android,定时器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c#实现metro文件压缩解压示例
2022-07-07 22:36:33
Mybatis如何获取最新插入数据的id
2023-02-26 08:15:20
android surfaceView实现播放视频功能
2022-12-11 12:55:04
Android仿微信图片上传带加号且超过最大数隐藏功能
2022-12-27 13:20:03
![](https://img.aspxhome.com/file/2023/0/128950_0s.png)
java JVM原理与常识知识点
2022-06-24 16:26:50
![](https://img.aspxhome.com/file/2023/8/118898_0s.jpg)
mybatis中查询结果为空时不同返回类型对应返回值问题
2023-02-15 10:50:31
C#创建WebService接口并连接的全过程
2023-04-07 07:47:21
![](https://img.aspxhome.com/file/2023/5/100085_0s.jpg)
Spring与Struts整合之让Spring管理控制器操作示例
2022-08-22 12:53:46
![](https://img.aspxhome.com/file/2023/2/84172_0s.png)
C#面向对象编程之猜拳游戏实现方法
2021-11-17 23:25:52
Maven分步详解多环境配置与应用流程
2021-12-28 07:52:14
![](https://img.aspxhome.com/file/2023/9/122309_0s.png)
Android实现底部支付弹窗效果
2022-06-12 13:39:43
![](https://img.aspxhome.com/file/2023/9/109229_0s.gif)
android实现widget时钟示例分享
2021-09-09 09:24:55
Java从服务端下载Excel模板文件的两种方法
2021-08-29 05:44:40
![](https://img.aspxhome.com/file/2023/5/67115_0s.jpg)
Android绘图常用方法汇总
2023-11-19 20:06:43
SpringBoot如何使用Fastjson解析Json数据
2023-11-25 11:55:58
![](https://img.aspxhome.com/file/2023/1/60041_0s.png)
解决Spring使用@MapperScan问题
2021-11-01 23:55:13
![](https://img.aspxhome.com/file/2023/3/73103_0s.jpg)
详解c# AutoMapper 使用方式
2022-05-25 20:51:38
![](https://img.aspxhome.com/file/2023/1/89761_0s.jpg)
Java 如何将表格数据导入word文档中
2023-08-13 10:38:55
详解java接口基础知识附思维导图
2023-11-09 10:07:28
![](https://img.aspxhome.com/file/2023/1/58861_0s.png)
C#使用log4net记录日志
2022-12-04 03:08:35
![](https://img.aspxhome.com/file/2023/8/106448_0s.jpg)