Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timer to call a function every n minutes?

I want to set up a timer in an Android application that will call a function after every 15/30/45 and n minutes when user login. But also it will stop timer when user log off. and timer begin from start if user login again. I want that option(15/30/45/n miutes) to be saved in database so that I can update list after sync.

Is Timer a good approach or I need to use alarm services? Or is there any system services required?

Is it possible to change previous doc/file in local phone database storage to new doc that is receiving through web server? is there any system services required to do so?

like image 952
Shweta Avatar asked Apr 17 '13 12:04

Shweta


1 Answers

Use following code to call your function every 15/30/45

 final Handler handler = new Handler();
Timer    timer = new Timer();
    TimerTask doAsynchronousTask = new TimerTask() {       
        @Override
        public void run() {
            handler.post(new Runnable() {
                @SuppressWarnings("unchecked")
                public void run() { 
                   try {
                        "Your function call  " 
                       }
                 catch (Exception e) {
                        // TODO Auto-generated catch block
                    }
                }
            });
        }
    };
    timer.schedule(doAsynchronousTask, 0, "Timer value"); 
like image 69
Prabu Avatar answered Oct 16 '22 22:10

Prabu