I need to do few requests to database and other jobs every one second. I'v used Service, Timer and AsyncTask class to do works in background. In first minutes it is ok and works without lag but after minutes it becomes slow and slower. How I can handle my jobs without this bad effect:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
PerformBackgroundTask performBackgroundTask = new PerformBackgroundTask();
performBackgroundTask.execute();
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 1000);
return Service.START_STICKY;
}
class PerformBackgroundTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... voids) {
// do database queries and other ...
return null;
}
}
I already used IntentService and AlarmManager for this and nothing changed. From the logcat the only 4 thread pool is used.