java Android:如何使用计时器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3486162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:02:04  来源:igfitidea点击:

Android: how to use a timer

javaandroidtimer

提问by Yorgo12345

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. ( this is for a tapping game )

我正在尝试单击一个 ImageView,让该 ImageView 交换图像一秒钟,然后返回到原始图像。(这是一个敲击游戏)

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? If not, then what is the ideal way?

有没有更好的办法?我的意思是,有没有 Android 特定的方法来做这样的事情?如果不是,那么理想的方式是什么?

thanks for all your help in advance guys! -g

提前感谢您的所有帮助!-G

回答by StefanMK

Here is my Android timer class which should work fine. It sends a signal every second. Change schedule() call is you want a different scheme.

这是我的 Android 计时器类,它应该可以正常工作。它每秒发送一个信号。更改 schedule() 调用是您想要不同的方案。

Note that you cannot change Android gui stuff in the timer thread, this is only allowed in the main thread. This is why you have to use a Handler to give the control back to the main thread.

请注意,您不能在计时器线程中更改 Android gui 内容,这仅在主线程中允许。这就是为什么您必须使用 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);        
    }
}

回答by Samuh

One way could be creating a Pipeline thread(a normal thread with 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

一种方法是创建一个流水线线程(一个带有 Looper.prepare() 的普通线程)。将延迟的消息发布到其消息循环。在消息处理程序中,交换图像。请参阅以下教程列表以了解所涉及的实体:
处理程序教程
处理程序:文档
Android 进入 Loopers(管道线程)和处理程序

Hope that helps.

希望有帮助。