I have two AsyncTasks doing network operations. I want to call them periodically (like after one min.). How do I do that? I dont think I can do it on the UI thread. Do i need to create a new thread? Is it possible to implemet this without AlarmManager/Service?
Basically I want to exectue these two statements periodically after one min.
new UploadAsyncTask().execute();
new DownloadAsyncTask().execute();
Thank you
Just use a timer.
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask task = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
new UploadAsyncTask().execute();
new DownloadAsyncTask().execute();
}
});
}
};
timer.schedule(task, 0, 1000); //it executes this every 1000ms
Related
I'm working on creating a timer in Java, and was wondering how I can use timer.cancel to then create a new timer which has a different interval.
My code looks something like this:
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
gameView.invalidate();
// timer.cancel(); - here, need to somehow restart timer with new interval
}
});
}
}, 0, TimerInterval.interval);
In another view, I'm modifying TimerInterval.interval, but this doesn't do anything/update the timer, because I need to somehow completely cancel the timer and create a new one, but I'm not sure how to do this.
Any help with this matter would be appreciated.
I want to make my listener to run muti-times, this my solution but i guess this will make a problem and number of threads in memory will be very large is it??
Timer timer = new Timer();
executor = Executors.newSingleThreadScheduledExecutor();
timer.schedule(new TimerTask() {
#Override
public void run() {
scheduleTime = Config.NILEDOX_INBOUND_AUTOIMPORT_ROTATE;
if(scheduleTime>=1){
/** setup single thread to run background service every x minutes **/
service = new AutoInboundMailImportService();
executor.scheduleWithFixedDelay(service,1,1,TimeUnit.MINUTES);
}
}}, 120000, 60000);
any idea??
I want to have a periodically task that should be run every 30 seconds. so I'm using ScheduledThreadPoolExecutor or Timer
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.scheduleWithFixedDelay(new MyTask(), 0, 30000, TimeUnit.MILLISECONDS);
class MyTask implements Runnable {
#Override
public void run() {
}
}
Here is Timer
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
// do something here
}
}, 0, 30000);
My question is: is there any differences if I start above code inside Service/IntentService or inside one activity. Those actions will be same or start inside service will better.
If you want to run this task even when your app is in backgroud then You should use a service or if you want to run in only when your app is in front so you may use it in activity
Hi I am working on TCP socket.
I can read data for every 1 sec. to achieve it I used TimerTask as shown in below code.
Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
finalizer = new Runnable() {
public void run() {
try {
if (navBool) {
runOnUiThread(new Runnable() {
public void run() {
new RetriveStock().execute(); // AsyncTask.
}
});
}
} catch (Exception e) {
}
}
};
handler.post(finalizer);
}
};
timer.schedule(doAsynchronousTask, 0, 1000);
For canceling this timer I used code as
timer.cancel();
timer = null;
handler.removeCallbacks(finalizer);
But it is not cancelling the timer. I do not know why.
Instead of calling timer.cancel(), you should be canceling the task that is assigned to that timer (doAsynchronousTask in your case). Since multiple TimerTasks can be assigned to one timer, calling timer.cancel() will not interfere with a currently running task.
From the Timer JavaDoc:
public void cancel()
Terminates this timer, discarding any currently scheduled tasks. Does
not interfere with a currently executing task (if it exists). Once a
timer has been terminated, its execution thread terminates gracefully,
and no more tasks may be scheduled on it.
So I'm trying to make a "live feed" essentially, and my code is shown below. Basically, I got "The Application Has Stopped Unexpectedly" error, so I debugged. When I debugged, it said "Timer.class not in android.jar". I know my refreshFeed() static method works perfectly fine (tested it without the timer), so it can't be that. Is there something I'm missing here? Any help is greatly appreciated!
Timer time = new Timer();
TimerTask refresh = new TimerTask(){
public void run(){
feedEntry.refreshFeed();
}
};
time.scheduleAtFixedRate(refresh, 0, 10000);
Both Timer and TimerTask present in java.util package.
What you need to is do is use handler.
Read about handler if you are new to android.
http://developer.android.com/reference/android/os/Handler.html
Handler handler = new Handler();
t = new Timer();
timeTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
feedEntry.refreshFeed();
}
});
}};
t.scheduleAtFixedRate(timeTask, 0, 1000);