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.