简体   繁体   中英

start a timerTask when activity is onPause()

is it possible to use a timerTask ,like:

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?

thanks in advance

You can start your TimerTask and inside it AsyncTast this way. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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