- Java android handler post and post delay
- Android: Issue using a handler and postDelayed()
- Handler.postAtTime vs Handler.postDelayed
- What is the difference between View.postDelayed() and Handler.postDelayed() on the main thread?
- Android Handler
- Using a Handler to execute code after a delayed amount of time
- HandlerThreads and communication between Threads
- Creating a Handler for the current Thread
- Creating a Handler for the main Thread (UI Thread)
- Send a Runnable from another Thread to the main Thread
- Creating a Handler for another HandlerThread and sending events to it
- Stop handler from execution
- Use Handler to create a Timer (similar to javax.swing.Timer)
- # Handler
- # HandlerThreads and communication between Threads
- # Creating a Handler for the current Thread
- # Creating a Handler for the main Thread (UI Thread)
- # Send a Runnable from another Thread to the main Thread
- # Creating a Handler for another HandlerThread and sending events to it
- # Use Handler to create a Timer (similar to javax.swing.Timer)
- # Using a Handler to execute code after a delayed amount of time
- # Stop handler from execution
- # Remarks
Java android handler post and post delay
If you want to do something in a worker thread, you need to do your own message handling (you could use synchronized methods in your thread that set member variables which the worker thread checks), or, if your thread is more of the event-driven variety, you could really consider adding a looper — but again, that is not a common practice. If what you want is Then why not just use since its the same. Update, forgot to add this: Solution 2: Looking at Handler source, it appears that there is : Which can be copied for what you want : Instead of calling , wrap your runnable in such a message you can then use with token as paramSolution 3: This is an old question, but the method version taking a token as an argument was added in API 28 : see https://developer.android.com/reference/android/os/Handler#postDelayed(java.lang.Runnable,%20java.lang.Object,%20long)
Android: Issue using a handler and postDelayed()
You can’t create a handler in a worker thread (unless it has a looper, which you normally never do). The handler needs a looper, since it needs a point that evaluates all incoming messages and calls the handler when necessary.
Your handler needs to be in the UI thread. If you want to do something in a worker thread, you need to do your own message handling (you could use synchronized methods in your thread that set member variables which the worker thread checks), or, if your thread is more of the event-driven variety, you could really consider adding a looper — but again, that is not a common practice.
I found a solution. Defining the handler in the onCreate method fixes it. Rest of the code is identical. Thanks 🙂
Android Java: «Cannot resolve» in postDelayed, updateTimeSlider is a C++ function used via JNI (Java Native Interface) using a statement at the bottom of this Java file that looks like this: private native void updateTimeSlider(); The JNI aspect of things is working fine, and updateTimeSlider() is the method I am trying to execute, not doUpdateTimeSlider.
Handler.postAtTime vs Handler.postDelayed
After looking at the source code, the token object eventually passes to the Message:
public final boolean postAtTime(Runnable r, Object token, long uptimeMillis) 308 < 309 return sendMessageAtTime(getPostMessage(r, token), uptimeMillis); 310 >private static Message getPostMessage(Runnable r, Object token) < 608 Message m = Message.obtain(); 609 m.obj = token;
public final boolean postDelayed(Runnable r, long delayMillis) 330
public final boolean postDelayed (Runnable r, Object token, long delay)
public final boolean postAtTime (Runnable r, Object token, long uptimeMillis)
Update, forgot to add this:
public final boolean sendMessageDelayed(Message msg, long delayMillis) 442 < 443 if (delayMillis < 0) < 444 delayMillis = 0; 445 >446 return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis); 447 >
Looking at Handler source, it appears that there is :
private final Message getPostMessage(Runnable r, Object token)
Which can be copied for what you want : Instead of calling postDelayed , wrap your runnable in such a message
sendMessageDelayed(getPostMessage(r, token), delayMillis);
you can then use removeCallbacks() with token as param
This is an old question, but the postDelayed method version taking a token as an argument was added in API 28 : see https://developer.android.com/reference/android/os/Handler#postDelayed(java.lang.Runnable,%20java.lang.Object,%20long)
For older API versions one still has to use postAtTime if a token is required to remove the callback later.
Java - postDelayed handler with loop, I did some research and tried some code including the code below from Android postDelayed Handler Inside a For Loop?. final Handler handler = new Handler(); java android android-handler. Share. Improve this question. Follow edited May 17, 2019 at 8:29. 5000); > >; // trigger first time handler.post
What is the difference between View.postDelayed() and Handler.postDelayed() on the main thread?
From the source, View.postDelayed() is simply using Handler.postDelayed() on an internal handler so there is no difference.
foo() may leak the Activity, you should use View.removeCallbacks() to minimize this chance.
Android - How to change/reset handler post delayed, You will post it like: handler.postDelayed (r, time_to_wait);. You could cancel it in the meantime by calling handler.removeCallbacks (r);. And then post it again, depending on your app's logic. Hope that helps. – Drew Apr 7, 2014 at 6:42 1 @MuhammedRefaat Yeap, you are right 🙂 I'm removing my flag. – Olga Konoreva …
Android Handler
A Handler can be easily used to execute code after a delayed amount of time. It is also useful for executing code repeatedly after a specified amount of time by calling the Handler.postDelayed() method again from within the Runnable's run() method.
Using a Handler to execute code after a delayed amount of time
Executing code after 1.5 seconds:
Handler handler = new Handler(); handler.postDelayed(new Runnable() < @Override public void run() < //The code you want to run after the time is up >>, 1500); //the time you want to delay in milliseconds
Executing code repeatedly every 1 second:
Handler handler = new Handler(); handler.postDelayed(new Runnable() < @Override public void run() < handler.postDelayed(this, 1000); >>, 1000); //the time you want to delay in milliseconds
HandlerThreads and communication between Threads
As Handler s are used to send Message s and Runnable s to a Thread's message queue it's easy to implement event based communication between multiple Threads. Every Thread that has a Looper is able to receive and process messages. A HandlerThread is a Thread that implements such a Looper , for example the main Thread (UI Thread) implements the features of a HandlerThread .
Creating a Handler for the current Thread
Handler handler = new Handler();
Creating a Handler for the main Thread (UI Thread)
Handler handler = new Handler(Looper.getMainLooper());
Send a Runnable from another Thread to the main Thread
new Thread(new Runnable() < public void run() < // this is executed on another Thread // create a Handler associated with the main Thread Handler handler = new Handler(Looper.getMainLooper()); // post a Runnable to the main Thread handler.post(new Runnable() < public void run() < // this is executed on the main Thread >>); > >).start();
Creating a Handler for another HandlerThread and sending events to it
// create another Thread HandlerThread otherThread = new HandlerThread("name"); // create a Handler associated with the other Thread Handler handler = new Handler(otherThread.getLooper()); // post an event to the other Thread handler.post(new Runnable() < public void run() < // this is executed on the other Thread >>);
Stop handler from execution
To stop the Handler from execution remove the callback attached to it using the runnable running inside it:
Runnable my_runnable = new Runnable() < @Override public void run() < // your code here >>; public Handler handler = new Handler(); // use 'new Handler(Looper.getMainLooper());' if you want this handler to control something in the UI // to start the handler public void start() < handler.postDelayed(my_runnable, 10000); >// to stop the handler public void stop() < handler.removeCallbacks(my_runnable); >// to reset the handler public void restart()
Use Handler to create a Timer (similar to javax.swing.Timer)
This can be useful if you're writing a game or something that needs to execute a piece of code every a few seconds.
import android.os.Handler; public class Timer < private Handler handler; private boolean paused; private int interval; private Runnable task = new Runnable () < @Override public void run() < if (!paused) < runnable.run (); Timer.this.handler.postDelayed (this, interval); >> >; private Runnable runnable; public int getInterval() < return interval; >public void setInterval(int interval) < this.interval = interval; >public void startTimer () < paused = false; handler.postDelayed (task, interval); >public void stopTimer () < paused = true; >public Timer (Runnable runnable, int interval, boolean started) < handler = new Handler (); this.runnable = runnable; this.interval = interval; if (started) startTimer (); >>
Timer timer = new Timer(new Runnable() < public void run() < System.out.println("Hello"); >>, 1000, true)
This code will print "Hello" every second.
# Handler
# HandlerThreads and communication between Threads
As Handler s are used to send Message s and Runnable s to a Thread's message queue it's easy to implement event based communication between multiple Threads. Every Thread that has a Looper is able to receive and process messages. A HandlerThread is a Thread that implements such a Looper , for example the main Thread (UI Thread) implements the features of a HandlerThread .
# Creating a Handler for the current Thread
Handler handler = new Handler();
# Creating a Handler for the main Thread (UI Thread)
Handler handler = new Handler(Looper.getMainLooper());
# Send a Runnable from another Thread to the main Thread
new Thread(new Runnable() public void run() // this is executed on another Thread // create a Handler associated with the main Thread Handler handler = new Handler(Looper.getMainLooper()); // post a Runnable to the main Thread handler.post(new Runnable() public void run() // this is executed on the main Thread > >); > >).start();
# Creating a Handler for another HandlerThread and sending events to it
// create another Thread HandlerThread otherThread = new HandlerThread("name"); // create a Handler associated with the other Thread Handler handler = new Handler(otherThread.getLooper()); // post an event to the other Thread handler.post(new Runnable() public void run() // this is executed on the other Thread > >);
# Use Handler to create a Timer (similar to javax.swing.Timer)
This can be useful if you're writing a game or something that needs to execute a piece of code every a few seconds.
import android.os.Handler; public class Timer private Handler handler; private boolean paused; private int interval; private Runnable task = new Runnable () @Override public void run() if (!paused) runnable.run (); Timer.this.handler.postDelayed (this, interval); > > >; private Runnable runnable; public int getInterval() return interval; > public void setInterval(int interval) this.interval = interval; > public void startTimer () paused = false; handler.postDelayed (task, interval); > public void stopTimer () paused = true; > public Timer (Runnable runnable, int interval, boolean started) handler = new Handler (); this.runnable = runnable; this.interval = interval; if (started) startTimer (); > >
Timer timer = new Timer(new Runnable() public void run() System.out.println("Hello"); > >, 1000, true)
This code will print "Hello" every second.
# Using a Handler to execute code after a delayed amount of time
Executing code after 1.5 seconds:
Handler handler = new Handler(); handler.postDelayed(new Runnable() @Override public void run() //The code you want to run after the time is up > >, 1500); //the time you want to delay in milliseconds
Executing code repeatedly every 1 second:
Handler handler = new Handler(); handler.postDelayed(new Runnable() @Override public void run() handler.postDelayed(this, 1000); > >, 1000); //the time you want to delay in milliseconds
# Stop handler from execution
To stop the Handler from execution remove the callback attached to it using the runnable running inside it:
Runnable my_runnable = new Runnable() @Override public void run() // your code here > >; public Handler handler = new Handler(); // use 'new Handler(Looper.getMainLooper());' if you want this handler to control something in the UI // to start the handler public void start() handler.postDelayed(my_runnable, 10000); > // to stop the handler public void stop() handler.removeCallbacks(my_runnable); > // to reset the handler public void restart() handler.removeCallbacks(my_runnable); handler.postDelayed(my_runnable, 10000); >
# Remarks
A Handler can be easily used to execute code after a delayed amount of time. It is also useful for executing code repeatedly after a specified amount of time by calling the Handler.postDelayed() method again from within the Runnable's run() method.