10

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?

endian
  • 4,761
  • 7
  • 32
  • 54
Shweta
  • 1,145
  • 5
  • 18
  • 35
  • You want to use the `AlarmManager` for delays of this magnitude, with `RTC` or `RTC_WAKEUP` option, depending on your needs. Apart from that, it's hard to say what you mean by "user logs on/off". Either way, don't use `Timer` on Android, it's almost always the wrong way. No Service required because `AlarmManager` takes care of everything for you. – class stacker Apr 17 '13 at 12:24
  • http://www.vogella.com/articles/AndroidServices/article.html – Nirmal Apr 17 '13 at 12:39

1 Answers1

40

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"); 
Prabu
  • 1,441
  • 15
  • 20