How can I create timer tick in Android?

Here is an example of Timer and Timer Task. Hope this helps.

final Handler handler = new Handler();
Timer timer = new Timer(false);
TimerTask timerTask = new TimerTask() {
    @Override
    public void run() {
        handler.post(new Runnable() {
            @Override
            public void run() {
                // Do whatever you want
            }
        });
    }
};
timer.schedule(timerTask, 1000); // 1000 = 1 second.

Maybe with a timer and a timertask?

See javadocs: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html

Yet receiving broadcasts is probably a more solid solution. See: Android - SMS Broadcast receiver


Use Timer.scheduleAtFixedRate() as follow:

final Handler handler = new Handler();
Timer timer = new Timer(false);
TimerTask timerTask = new TimerTask() {
    @Override
    public void run() {
        handler.post(new Runnable() {
            @Override
            public void run() {
                GetSMS();
            }
        });
    }
};

timer.scheduleAtFixedRate(timerTask, 5000, 5000); // every 5 seconds.

Tags:

Android