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

半城伤御伤魂 提交于 2019-12-09 09:20:20

问题


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?


回答1:


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"); 


来源:https://stackoverflow.com/questions/16059754/how-to-set-timer-to-call-a-function-every-n-minutes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!