Android 三种实现定时器详解及实现方法

作者:yhm2046 时间:2021-11-18 21:53:42 

方法一:Handler+Thread


package com.xunfang.handerDemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

/**
* handler定时器
*
* @author Smalt
*
*/
public class HanderDemoActivity extends Activity {
TextView tvShow;
private int i = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 tvShow = (TextView) findViewById(R.id.tv_show);
 new Thread(new ThreadShow()).start();
}

// handler类接收数据
Handler handler = new Handler() {
 public void handleMessage(Message msg) {
  if (msg.what == 1) {
   tvShow.setText(Integer.toString(i++));
   System.out.println("receive....");
  }
 };
};

// 线程类
class ThreadShow implements Runnable {

@Override
 public void run() {
  // TODO Auto-generated method stub
  while (true) {
   try {
    Thread.sleep(1000);
    Message msg = new Message();
    msg.what = 1;
    handler.sendMessage(msg);
    System.out.println("send...");
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    System.out.println("thread error...");
   }
  }
 }
}
}

方法二:Handler类自带的postDelyed


package com.xunfang.handerDemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

/**
* handler定时器使用postDelyed实现
*
* @author Smalt
*
*/
public class HanderDemoActivity extends Activity {
TextView tvShow;
private int i = 0;
private int TIME = 1000;

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 tvShow = (TextView) findViewById(R.id.tv_show);
 handler.postDelayed(runnable, TIME); //每隔1s执行
}

Handler handler = new Handler();
Runnable runnable = new Runnable() {

@Override
 public void run() {
  // handler自带方法实现定时器
  try {
   handler.postDelayed(this, TIME);
   tvShow.setText(Integer.toString(i++));
   System.out.println("do...");
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   System.out.println("exception...");
  }
 }
};

}

方法三:Handler+Timer+TimerTask


package com.xunfang.handerDemo;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

/**
* 定时器实现:Handler+Timer+TimerTask
*
* @author Smalt
*
*/
public class HanderDemoActivity extends Activity {
TextView tvShow;
private int i = 0;
private int TIME = 1000;

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 tvShow = (TextView) findViewById(R.id.tv_show);
 timer.schedule(task, 1000, 1000); // 1s后执行task,经过1s再次执行
}

Handler handler = new Handler() {
 public void handleMessage(Message msg) {
  if (msg.what == 1) {
   tvShow.setText(Integer.toString(i++));
  }
  super.handleMessage(msg);
 };
};
Timer timer = new Timer();
TimerTask task = new TimerTask() {

@Override
 public void run() {
  // 需要做的事:发送消息
  Message message = new Message();
  message.what = 1;
  handler.sendMessage(message);
 }
};
}
标签:Android,定时器
0
投稿

猜你喜欢

  • spring boot整合RabbitMQ实例详解(Fanout模式)

    2022-08-18 18:52:30
  • SpringBoot Test类注入失败的解决

    2023-05-20 13:59:40
  • c#创建windows服务(Windows Services)详细步骤

    2023-06-11 19:05:22
  • 剑指Offer之Java算法习题精讲链表专项训练

    2023-11-29 16:31:48
  • java中Class.forName方法的作用详解

    2021-07-08 21:47:31
  • C#实现鼠标消息捕获

    2021-06-14 20:38:38
  • Mybatis参数传递示例代码

    2023-05-20 09:43:08
  • java反射拼接方法名动态执行方法实例

    2022-12-07 00:44:28
  • 在IntelliJ IDEA中为自己设计的类库生成JavaDoc的方法示例

    2023-11-25 09:49:02
  • 解决Spring Security中AuthenticationEntryPoint不生效相关问题

    2022-11-29 06:53:09
  • 解析maven的用法和几个常用的命令(推荐)

    2022-04-16 23:31:15
  • Java求两集合中元素交集的四种方法对比分析

    2023-08-23 09:24:56
  • slf4j jcl jul log4j1 log4j2 logback各组件系统日志切换

    2023-08-08 13:00:41
  • 半小时实现Java手撸网络爬虫框架(附完整源码)

    2022-11-23 15:59:35
  • 浅谈java实现背包算法(0-1背包问题)

    2022-04-28 15:23:43
  • SpringBoot整合canal实现数据同步的示例代码

    2022-05-07 19:51:24
  • Java实现中国象棋的示例代码

    2021-08-10 21:56:03
  • 在Java中使用redisTemplate操作缓存的方法示例

    2023-07-04 09:03:28
  • C#基于TimeSpan实现倒计时效果的方法

    2021-10-19 00:06:04
  • java后台接受到图片后保存方法

    2023-06-03 09:23:04
  • asp之家 软件编程 m.aspxhome.com