Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a interval timer in Java android

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?

like image 835
Hans Fredrik Avatar asked Oct 27 '12 12:10

Hans Fredrik


People also ask

How do you make a 1 second timer in Java?

You can start the timer with one of the schedule methods like this: timer. schedule(new TimerTask() { @Override public void run() { //do your stuff here... } }, 0, 1000); This will start your timer with a delay of 0 milliseconds and repeat it after 1000 milliseconds.

How do you set a timer on android?

onTick(long millisUntilFinished ) - In this method we have to pass countdown mill seconds after done countdown it will stop Ticking. onFinish() - After finish ticking, if you want to call any methods or callbacks we can do in onFinish(). start() - It is used to call countdown timer.

What is timer class in android?

A facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals.

How is time interval defined in Java?

The code can be used in various ways: // interval from start to end DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0); DateTime end = new DateTime(2005, 1, 1, 0, 0, 0, 0); Interval interval = new Interval(start, end); Accessing other objects is easy: Interval interval = ...


1 Answers

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.

like image 152
Chris.Jenkins Avatar answered Nov 15 '22 20:11

Chris.Jenkins