android android - 4 months ago 46
Android Question

How can I create timer tick in Android?

I have this method

public void GetSMS(){
//in this method I read SMS in my app inbox,
//If have new SMS create notification
}


for this I think create timer tick method and every 5 sec call GetSMS()

How can I create a correct method for that ?

Answer

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.
Comments