Making a interval timer in Java android

2019-01-22 04:13发布

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?

1条回答
劫难
2楼-- · 2019-01-22 04:37

I would always recommend using a 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.

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

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.

The two main uses are:

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

Simple use!

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.

查看更多
登录 后发表回答