如何设置timer每n分钟调用一次函数?

时间:2022-12-17 01:08:17

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.

我想在Android应用程序中设置一个计时器,在用户登录后每15/30/45和n分钟调用一次函数。但当用户注销时,它也会停止计时器。如果用户再次登录,计时器将从开始开始。我希望将这个选项(15/30/45/n miutes)保存到数据库中,以便在同步后更新列表。

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?

是否可以将本地电话数据库存储中的先前的doc/文件更改为通过web服务器接收的新的doc ?是否有任何系统服务需要这样做?

1 个解决方案

#1


39  

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

使用以下代码每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"); 

#1


39  

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

使用以下代码每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");