简体   繁体   English

当活动为onPause()时启动一个timerTask

[英]start a timerTask when activity is onPause()

is it possible to use a timerTask ,like: 是否可以使用timerTask ,例如:

timer = new Timer();
            task = new TimerTask() {
                @Override
                public void run() {
                    handler.post(new Runnable() {
                        public void run() {
                            new AsyncTask().execute();
                        }
                    });
                }
            };
            timer.schedule(task, 0, 4000); 

I need to start a periodically task when activity is on background ,instead of using a Service , when activity is onPause() and to prevent to be killed by the system? 我需要在活动处于后台时启动定期任务,而不是在活动为onPause()时使用Service并防止被系统杀死?

thanks in advance 提前致谢

You can start your TimerTask and inside it AsyncTast this way. 您可以通过这种方式启动TimerTask并将其放入AsyncTast。 They will run as long as your application process is running, but they will not prevent killing of your Activity and Application. 只要您的应用程序进程正在运行,它们就会运行,但是它们不会阻止您的活动和应用程序被杀死。 When considering which activity or process to kill android does not take into account threads or timers running inside your app, only Activities, Services, BroadcastReceivers etc count. 当考虑杀死哪个活动或进程时,不考虑应用程序内部运行的线程或计时器,只有活动,服务,BroadcastReceivers等才算在内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM