Android实现计时与倒计时的方法汇总
作者:fengyeNom1 时间:2023-11-09 19:29:28
方法一
Timer与TimerTask(Java实现)
public class timerTask extends Activity{
private int recLen = 11;
private TextView txtView;
Timer timer = new Timer();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);
timer.schedule(task, 1000, 1000); // timeTask
}
TimerTask task = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() { // UI thread
@Override
public void run() {
recLen--;
txtView.setText(""+recLen);
if(recLen < 0){
timer.cancel();
txtView.setVisibility(View.GONE);
}
}
});
}
};
}
方法二
TimerTask与Handler(不用Timer的改进型)
public class timerTask extends Activity{
private int recLen = 11;
private TextView txtView;
Timer timer = new Timer();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);
timer.schedule(task, 1000, 1000); // timeTask
}
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
switch (msg.what) {
case 1:
txtView.setText(""+recLen);
if(recLen < 0){
timer.cancel();
txtView.setVisibility(View.GONE);
}
}
}
};
TimerTask task = new TimerTask() {
@Override
public void run() {
recLen--;
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
}
方法三
Handler与Message(不用TimerTask)
public class timerTask extends Activity{
private int recLen = 11;
private TextView txtView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);
Message message = handler.obtainMessage(1); // Message
handler.sendMessageDelayed(message, 1000);
}
final Handler handler = new Handler(){
public void handleMessage(Message msg){ // handle message
switch (msg.what) {
case 1:
recLen--;
txtView.setText("" + recLen);
if(recLen > 0){
Message message = handler.obtainMessage(1);
handler.sendMessageDelayed(message, 1000); // send message
}else{
txtView.setVisibility(View.GONE);
}
}
super.handleMessage(msg);
}
};
}
方法四
Handler与Thread(不占用UI线程)
public class timerTask extends Activity{
private int recLen = 0;
private TextView txtView;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);
new Thread(new MyThread()).start(); // start thread
}
final Handler handler = new Handler(){ // handle
public void handleMessage(Message msg){
switch (msg.what) {
case 1:
recLen++;
txtView.setText("" + recLen);
}
super.handleMessage(msg);
}
};
public class MyThread implements Runnable{ // thread
@Override
public void run(){
while(true){
try{
Thread.sleep(1000); // sleep 1000ms
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}catch (Exception e) {
}
方法五
Handler与Runnable(最简单型)
public class timerTask extends Activity{
private int recLen = 0;
private TextView txtView;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);
handler.postDelayed(runnable, 1000);
}
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
recLen++;
txtView.setText("" + recLen);
handler.postDelayed(this, 1000);
}
};
}
计时与倒计时
方法1,方法2和方法3,都是倒计时
方法4,方法5,都是计时
计时和倒计时,都可使用上述方法实现(代码稍加改动)
UI线程比较
方法1,方法2和方法3,都是在UI线程实现的计时;
方法4和方法5,是另开Runnable线程实现计时
实现方式比较
方法1,采用的是Java实现,即Timer和TimerTask方式;
其它四种方法,都采用了Handler消息处理
推荐使用
如果对UI线程交互要求不很高,可以选择方法2和方法3
如果考虑到UI线程阻塞,严重影响到用户体验,推荐使用方法4,另起线程单独用于计时和其它的逻辑处理
方法5,综合了前几种方法的优点,是最简的
以上所述是小编给大家介绍的Android实现计时与倒计时的方法汇总值网站的支持!
来源:http://blog.csdn.net/fengyenom1/article/details/72870572
标签:android,计时,倒计时
0
投稿
猜你喜欢
深入理解Android中Scroller的滚动原理
2022-10-01 18:53:01
使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)
2023-04-24 07:35:45
C#中Convert.ToString和ToString的区别分析
2023-02-04 22:18:06
Struts2返回json格式数据代码实例
2023-10-12 13:15:32
java实现Yaml转Json示例详解
2023-12-06 22:58:34
Java实现控制台输出两点间距离
2023-05-18 11:49:11
详解idea从git上拉取maven项目详细步骤
2023-04-23 08:51:56
Android 实现沉浸式状态栏的方法
2023-05-02 21:42:11
Java 事务注解@Transactional回滚(try catch、嵌套)问题
2021-05-29 17:54:43
C#中Dictionary泛型集合7种常见的用法
2021-08-07 04:47:17
SpringBoot实现动态配置及项目打包部署上线功能
2021-07-13 21:36:53
C#实现简单的天气预报示例代码
2022-03-22 22:52:59
Freemarker如何生成树形导航菜单(递归)
2023-02-24 13:52:16
详解JavaScript中的函数声明和函数表达式
2023-04-26 01:56:07
深入浅出讲解Java集合之Map接口
2023-10-14 20:52:46
实例讲解Android应用开发中TabHost的使用要点
2023-11-05 06:52:14
Android中Intent习惯用法
2021-10-10 08:48:22
Android自定义覆盖层控件 悬浮窗控件
2021-10-21 01:14:40
Android使用 Retrofit 2.X 上传多文件和多表单示例
2023-08-06 03:48:25
Intellij IDEA 2018配置Java运行环境的方法步骤
2023-06-08 09:56:50