简体   繁体   English

Android:如何使用计时器

[英]Android: how to use a timer

this is my first post.. 这是我的第一篇文章..

so I'm learning Android & Java (coming from Actionscript), and I'm working on a project where : 所以我正在学习Android和Java(来自Actionscript),我正在研究一个项目,其中:

I'm trying to click an ImageView, have that ImageView swap images for a second, then return to the original image. 我正在尝试单击ImageView,让ImageView交换图像一秒钟,然后返回到原始图像。 ( this is for a tapping game ) (这是一个攻击游戏)

sounds easy enough, right? 听起来很容易,对吧? I've spent the whole day trying to get a standard Java Timer / TimerTask to work.. no luck.. 我花了一整天的时间试图让标准的Java Timer / TimerTask工作..没有运气..

is there a better way? 有没有更好的办法? I mean, is there an Android specific way to do something like this? 我的意思是,是否有Android特定的方式来做这样的事情? If not, then what is the ideal way? 如果没有,那么理想的方式是什么?

thanks for all your help in advance guys! 感谢您提前帮助的所有人! -g -G

Here is my Android timer class which should work fine. 这是我的Android计时器类,应该可以正常工作。 It sends a signal every second. 它每秒发送一个信号。 Change schedule() call is you want a different scheme. 更改schedule()调用是否需要不同的方案。

Note that you cannot change Android gui stuff in the timer thread, this is only allowed in the main thread. 请注意,您无法在计时器线程中更改Android gui内容,这仅在主线程中允许。 This is why you have to use a Handler to give the control back to the main thread. 这就是您必须使用Handler将控件返回主线程的原因。

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.os.Handler;
import android.os.Message;

public class SystemTimerAndroid {
    private final Timer clockTimer;

    private class Task extends TimerTask {
        public void run() {
            timerHandler.sendEmptyMessage(0);
        }
    }

    private final Handler timerHandler = new Handler() {
        public void handleMessage (Message  msg) {
            // runs in context of the main thread
            timerSignal();
        }
    };

    private List<SystemTimerListener> clockListener = new ArrayList<SystemTimerListener>();

    public SystemTimerAndroid() {
        clockTimer = new Timer();
        clockTimer.schedule(new Task(), 1000, 1000);
    }

    private void timerSignal() {
        for(SystemTimerListener listener : clockListener)
            listener.onSystemTimeSignal();      
    }

    public void killTimer() {
        clockTimer.cancel();
    }

    @Override
    public void addListener(SystemTimerListener listener) {
        clockListener.add(listener);        
    }
}

One way could be creating a Pipeline thread(a normal thread with Looper.prepare()). 一种方法是创建一个Pipeline线程(一个带有Looper.prepare()的普通线程)。 Post delayed messages to its message loop. 将延迟消息发布到其消息循环。 In the Message Handler, swap the images. 在消息处理程序中,交换图像。 See the following list of tutorials to understand the entities involved: 请参阅以下教程列表以了解所涉及的实体:
Handler Tutorial 处理程序教程
Handler: documentation 处理程序:文档
Android guts into Loopers(Pipeline threads) and Handlers Android进入Loopers(管道线程)和处理程序

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM