What is the best way to delay a method call without freezing the UI or running of the program? I want to display circles on the screen every 5 seconds but in those 5 seconds, other existing circles will be changing size so the drawcircle method has to be called every 5 seconds but other code has to be able to run too.
Use Handler for it:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//any delayed code
}
}, 5000);
Causes the Runnable to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached. The time-base is in milliseconds, in eg above it is 5000 milliseconds.
The postDelayed takes two parameters:
The Runnable that will be executed.
The delay (in milliseconds) until the Runnable will be executed.
Use Handler's method called postDelayed.
For more info, read this.
Related
I have a set of instructions that needs to be executed repeatedly.
The repeat delay is variable (the delay is taken from a file).
The repeat time must be very precise (milliseconds precision).
I have tried to use a Handler with postDelayed but the accuracy is lost in time. This happens because the repeat frequency is more than 10 times/second.
Any idea is more than welcomed!
Here is the Handler example:
new Runnable() {
#Override
public void run() {
mHandler.postDelayed(this, delay);
barometerResult.gotBarometer(barometerValueModelList.get(i).getBarometerValue());
}
}.run();
I don't think it's a good idea using Handler, it runs in the UI thread with many other things to do in the queue, so that's not guarantee your runnable code would be executed so precisely.
If your work in the Runnable is just some logic task not involved in update UI.Consider using a Timer to do that but be ware that it will run on a separated thread.
I am having a service in my application that puts a runnable (in another java file) in a thread and starts it.
That is working fine for once, but i want it to be repetitive due to a certain period.
I need a good way to handle that.
Reason why I didn't use the answers to other questions is that I don't want it to repeat infinity nor I know how many times it'll repeat the task. It'll simply stop due to a button click in the UI.
I was thinking of using a loop with a sleep and if statement. But I think that's really bad design for my application. Is there a standard way for doing such thing?
Thanks...
You can use a handler that somehow acts like a timer but I think it is better for your situation.
You initialize it like this:
Handler delayhandler = new Handler();
Set the time it fires like this (in ms):
delayhandler.postDelayed(mUpdateTimeTask, 500);
And it calls this:
private Runnable mUpdateTimeTask = new Runnable()
{ public void run()
{ // Todo
// This line is necessary for the next call
delayhandler.postDelayed(this, 100);
}
}
You can also remove the next call with:
delayhandler.removeCallbacks(mUpdateTimeTask);
Use a TimerTask and have it execute your thread/method.
http://android.okhelp.cz/timer-simple-timertask-java-android-example/
http://developer.android.com/reference/java/util/TimerTask.html - You can use the Cancel() method to stop the TimerTask from executing.
Use the Timer it will run the thread after a given time period and when you want to stop just stop timer or set it to infinite time period.
I am looking for the method to update the screen at a constant rate, say every 50mSec.
In the embedded world I would configure a timer to trigger an interrupt every 50mSec and the ISR would handle the update. What I have in mind for Android is to have a FrameLayout with a number of view items registered to react to some sort of callback from a timer function.
Hopefully someone will understand my question and point me in the right direction.
You may use handler.postDelayed in a Runnable to call it after certain time period.
For example:
handler.post(new Runnable() {
#Override
public void run() {
handler.postDelayed(this, 50);
//do your task
}
});
Make sure you handler is attached to a separate (non-UI) thread and when it comes to update UI elements you always do it on a UI thread.
I am a new Android developer. I am using the Handler class to schedule some operations. So I am creating runnable objects that calls some of my instance methods.
But I have a problem. Sometimes my run() method in Runnable object is called twice.
What could be the problem??
and there is the code
//deneme is a Handler.
deneme.postDelayed(new Runnable() {
#Override
public void run()
{
randomOyna();
//the instance method that I call.
}
}, 1000);
If don't schedule your Handler to run on another Thread than the UI-thread, there might be a hidden delay in the execution because your Runnable will also run on the UI-thread and thus will only be allowed to run when there is "time" for it. With this hidden delay, it might seem like it is run twice but in reality it's just and over-delayed running before a regular delayed Runnable.
Can't see a mistake just by looking the hint you gave us... But you might try plain old java to run threads instead of handler... Good luck...
Look here for more details
If I've got it right a handler works like a queue. So my simple question is if I first postDelayed and after that do I regular post. Will the second post run after the first delayed post?
handler.postDelayed(someMethod(), 10000);
handler.post(someOtherMethod());
Will the someOtherMethod() run after someMethod()
No it won't. The second one will be performed immediately when all pending handler requests (so on the UI thread) are done. The first will be appended after 10 seconds.
Hint: The handler takes a Runnable, so it looks like that:
handler.post(new Runnable {
public void run() {
doSomething();
}
}
Update based on comment:
The handler invokes the posted runnable on the thread on which new Handler() was called.
No, 2nd post will run as soon as possible (before 1st).
No, handler posts are asynchronous. what handler.post() does it that it adds the runnable to the message queue. handler.postDelayed() adds the runnable to the message queue immediately , but the timer will be set to delayed ms , after which the runnable will be executed.
so all post does is , to add the runnable to the message queue.