惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Latest news
Latest news
T
Troy Hunt's Blog
V
Vulnerabilities – Threatpost
L
LINUX DO - 热门话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
V
V2EX
博客园 - 司徒正美
B
Blog RSS Feed
AWS News Blog
AWS News Blog
MyScale Blog
MyScale Blog
Scott Helme
Scott Helme
Cisco Talos Blog
Cisco Talos Blog
Last Week in AI
Last Week in AI
NISL@THU
NISL@THU
博客园 - Franky
P
Proofpoint News Feed
博客园_首页
C
CERT Recently Published Vulnerability Notes
雷峰网
雷峰网
S
Schneier on Security
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
WordPress大学
WordPress大学
The Hacker News
The Hacker News
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
A
Arctic Wolf
Microsoft Azure Blog
Microsoft Azure Blog
T
The Exploit Database - CXSecurity.com
Engineering at Meta
Engineering at Meta
罗磊的独立博客
T
The Blog of Author Tim Ferriss
D
Darknet – Hacking Tools, Hacker News & Cyber Security
I
Intezer
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
K
Kaspersky official blog
SecWiki News
SecWiki News
云风的 BLOG
云风的 BLOG
美团技术团队
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 【当耐特】
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Security Latest
Security Latest
C
Cyber Attacks, Cyber Crime and Cyber Security
B
Blog
S
Security Affairs

博客园 - witer666

PHP API 框架开发的学习 [android开发必备] Android开发者社区汇总 微博feed系统的push和pull模式和时间分区拉模式架构探讨 一个mysql小技巧 redhat安装memcacheQ php empty问题 Linux PHP连接MSSQL Curl参数一览 Redhat Memcache UDF安装配置 Redhat Mongodb学习笔记 PHP使用技巧 位运算符C语言 Redhat Lighttpd安装配置 HTTP 状态代码表示什么意思? PHP正则表达式学习参考的文章 Redhat Cacti安装配置 Percona-Server5.5安装配置 50点提高PHP编程效率 引入缓存提升性能 wget使用
android定时器
witer666 · 2013-01-12 · via 博客园 - witer666

Android开发中,定时器一般有以下3实现方法

一、采用Handler与线程的sleep(long)方法
二、采用Handler的postDelayed(Runnable, long)方法
三、采用Handler与timer及TimerTask结合的方法

下面逐一介绍:

一、采用Handle与线程的sleep(long)方法

Handler主要用来处理接受到的消息。这只是最主要的方法,当然Handler里还有其他的方法供实现,有兴趣的可以去查API,这里不过多解释。

1. 定义一个Handler类,用于处理接受到的Message。

? Handler handler = new Handler() {

    public void handleMessage(Message msg) {

        // 要做的事情

        super.handleMessage(msg);

    }

};

2. 新建一个实现Runnable接口的线程类,如下:

? public class MyThread implements Runnable {

    @Override

    public void run() {

        // TODO Auto-generated method stub

        while (true) {

            try {

                Thread.sleep(10000);// 线程暂停10秒,单位毫秒

                Message message = new Message();

                message.what = 1;

                handler.sendMessage(message);// 发送消息

            } catch (InterruptedException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

    }

}

3. 在需要启动线程的地方加入下面语句:

?

1

new Thread(new MyThread()).start();

4. 启动线程后,线程每10s发送一次消息。

二、采用HandlerpostDelayed(Runnable, long)方法

这个实现比较简单一些。

1. 定义一个Handler类

Handler handler=new Handler();

Runnable runnable=new Runnable() {

    @Override

    public void run() {

        // TODO Auto-generated method stub

        //要做的事情

        handler.postDelayed(this, 2000);

    }

};

2. 启动计时器

?

1

handler.postDelayed(runnable, 2000);//每两秒执行一次runnable.

3. 停止计时器

?

1

handler.removeCallbacks(runnable);

三、采用HandlertimerTimerTask结合的方法

1. 定义定时器定时器任务及Handler句柄

private final Timer timer = new Timer();

private TimerTask task;

Handler handler = new Handler() {

    @Override

    public void handleMessage(Message msg) {

        // TODO Auto-generated method stub

        // 要做的事情

        super.handleMessage(msg);

    }

};

2. 初始化计时器任务

?

1

2

3

4

5

6

7

8

9

task = new TimerTask() {

    @Override

    public void run() {

        //   TODO Auto-generated method stub

        Message   message = new Message();

        message.what   = 1;

        handler.sendMessage(message);

    }

};

3. 启动定时器

?

1

timer.schedule(task, 2000, 2000);

简要说一下上面三步提到的一些内容:

1. 定时器任务(TimerTask)顾名思义,就是说当定时器到达指定的时间时要做的工作,这里是想Handler发送一个消息,由Handler类进行处理。

2. java.util.Timer.schedule(TimerTask task, long delay):这个方法是说,dalay/1000秒后执行task.只执行一次。
java.util.Timer.schedule(TimerTask task, long delay, long period):这个方法是说,delay/1000秒后执行task,然后进过period/1000秒再次执行task,这个用于循环任务,执行无数次,当然,你可以用timer.cancel();取消计时器的执行。