Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create timer tick in Android?

Tags:

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 ?

like image 223
android Avatar asked Jan 17 '13 16:01

android


1 Answers

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.
like image 193
Farooq Arshed Avatar answered Oct 12 '22 15:10

Farooq Arshed