欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android三种实现定时器的方法

程序员文章站 2022-03-23 14:05:34
方法一、使用handler和thread(线程)实现定时器 复制代码 代码如下: package com.xunfang.handerdemo; import and...

方法一、使用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;
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() {
            while (true) {
                try {
                    thread.sleep(1000);
                    message msg = new message();
                    msg.what = 1;
                    handler.sendmessage(msg);
                    system.out.println("send...");
                } catch (exception e) {
                    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;
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三个android类实现定时器

复制代码 代码如下:

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;
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);
        }
    };
}