在 Java android 中制作一个间隔计时器 [英] Making a interval timer in Java android

查看:30
本文介绍了在 Java android 中制作一个间隔计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划使用计时器创建一个间隔应用程序.它应该只是最基本的所以当我了解了基础知识后,我将不得不添加更多内容.我想要实现的是选择间隔应该持续的分钟数,但是这个间隔应该持续多少次.就像一个持续 1 分钟并进行 8 次的间隔.问题是哪个计时器最适合使用?我在 Android Countdown Timer 上试过我,它似乎工作.但是还有另一个更好的吗?

I have plans to create an interval app using timers. It should just be the most basic So I'll have to add some more when I've understood the basics. What I want to achieve is to select the number of minutes an interval should last, yet how many times this interval should go. Like a interval that last 1 minute and goes 8 times. The question is which timer is best to use? I have tried me on the Android Countdown Timer and it seems to work. But is there another one which is better?

推荐答案

我总是建议使用 Handler.

它比内置类要多一些工作,但我发现它的效率要高得多,而且您可以更好地控制它.

It's a little more work than the built in classes, but I find that it is vastly more efficient and you have more control over it.

Handler 是一个类,它会在特定的 Looper/Thread 上处理代码执行,默认情况下,它是在其中创建的线程,否则您可以指定 Handler通过将 Looper 传递给 Handler 构造函数来执行其代码,例如 - new Handler(Looper.getMainLooper());

The Handler is a class that will handle code execution over a specific Looper / Thread by default, the Thread it is created in, Otherwise you can specify where the Handler executes its code by passing in the Looper to the Handler constructor like - new Handler(Looper.getMainLooper());

我推荐 Looper 的原因是因为你有更高的控制灵活性,因为它比 TimerTask 方法的抽象稍低.

The reason I would recommend the looper is because you have a higher flexibility of control, as it is a slightly lower down abstraction over the TimerTask methods.

通常,它们对于跨线程执行代码非常有用.例如.对于跨线程管道数据很有用.

Generally they are very useful for executing code across threads. E.g. useful for piping data across threads.

两个主要用途是:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    final Handler h = new Handler();
    h.postDelayed(new Runnable()
    {
        private long time = 0;

        @Override
        public void run()
        {
            // do stuff then
            // can call h again after work!
            time += 1000;
            Log.d("TimerExample", "Going for... " + time);
            h.postDelayed(this, 1000);
        }
    }, 1000); // 1 second delay (takes millis)
}

使用简单!

或者您可以使用消息来减少对象的创建.如果您正在考虑高速更新 UI 等 - 这将减少垃圾收集器的压力.

Or you can use messages, which reduce object creation. If you are thinking about high speed updating UI etc - this will reduce pressure on the garbage collector.

class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        MyTimers timer = new MyTimers();
        timer.sendEmptyMessage(MyTimers.TIMER_1);
        timer.sendEmptyMessage(MyTimers.TIMER_2);

    }


    public static class MyTimers extends Handler
    {

        public static final int TIMER_1 = 0;
        public static final int TIMER_2 = 1;

        @Override
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case TIMER_1:
                    // Do something etc.
                    Log.d("TimerExample", "Timer 1");
                    sendEmptyMessageDelayed(TIMER_1, 1000);
                    break;
                case TIMER_2:
                    // Do another time update etc..
                    Log.d("TimerExample", "Timer 2");
                    sendEmptyMessageDelayed(TIMER_2, 1000);
                    break;
                default:
                    removeMessages(TIMER_1);
                    removeMessages(TIMER_2);
                    break;
            }
        }
    }
}

显然,这不是一个完整的实现,但它应该给你一个良好的开端.

Obviously this is not a full implementation but it should give you a head start.

这篇关于在 Java android 中制作一个间隔计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆