Start Asynctask In Timertask
I have a timer that I want to start an AsyncTask when the countdown is done. If I put the execution of it in a handler it loops it and starts it many times. And if I dont put it in
Solution 1:
AsyncTask is supposed to run on UI thread only. In your case, seems like you are not running it properly on a UI thread.
Perhaps try it like this:
timer.schedule(new ListUpdate(), helper.nextListUpdate.get(0));
classListUpdateextendsTimerTask {
Looper looper = Looper.getMainLooper();
looper.prepareMainLooper();
private Handler mHandler = new Handler(looper);
publicvoidrun() {
mHandler.post(new Runnable() {
publicvoidrun() {
AsyncTask<Integer, Void, Boolean> task = new updateList();
task.execute();
}
});
}
}
Solution 2:
By adding a handler outside of the TimerTask which I call from the TimerTask I could make it work!
finalHandlerhandler=newHandler() {
publicvoidhandleMessage(Message msg) {
RelativeLayoutrl_header= (RelativeLayout)findViewById(R.id.rl_header);
Desktopdesktop= helper.getDesktop();
try {
desktop.inflate(ll, rl_header, banners, DesktopApp.this);
Collections.sort(helper.nextListUpdate);
helper.nextListUpdate.remove(0);
timer = newTimer();
if (helper.nextListUpdate.size() > 0) timer.schedule(newListUpdate(), helper.nextListUpdate.get(0));
} catch (Exception e) {
e.printStackTrace();
}
}
};
classListUpdateextendsTimerTask {
publicvoidrun() {
DesktopApp.this.runOnUiThread(newRunnable() {
@Overridepublicvoidrun() {
handler.sendEmptyMessage(0);
}
});
}
}
Post a Comment for "Start Asynctask In Timertask"