What is the relationship between Looper, Handler and MessageQueue in Android?

A Looper is a message handling loop: it reads and processes items from a MessageQueue. The Looper class is usually used in conjunction with a HandlerThread (a subclass of Thread). A Handler is a utility class that facilitates interacting with a Looper—mainly by posting messages and Runnable objects to the thread’s MessageQueue. When a Handler … Read more

Android – Hold Button to Repeat Action

This is more independent implementation, usable with any View, that supports touch event: import android.os.Handler; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; /** * A class, that can be used as a TouchListener on any view (e.g. a Button). * It cyclically runs a clickListener, emulating keyboard-like behaviour. First * click is fired immediately, … Read more

Does jQuery have a handleout for .delegate(‘hover’)?

User113716’s great answer will no longer work in jQuery 1.9+, because the pseudo-event hover is no longer supported (upgrade guide). Also since jQuery 3.0 delegate() for binding events is officially deprecated, so please use the new on()(docs) for all event binding purposes. You can easily migrate user113716‘s solution by replacing hover with mouseenter mouseleave and … Read more

Handlers and memory leaks in Android

I recently updated something similar in my own code. I just made the anonymous Handler class a protected inner class and the Lint warning went away. See if something like the below code will work for you: public class MyGridFragment extends Fragment{ static class MyInnerHandler extends Handler{ WeakReference<MyGridFragment> mFrag; MyInnerHandler(MyGridFragment aFragment) { mFrag = new … Read more

What’s the difference between Event Listeners & Handlers in Java?

There’s no formally defined difference between listeners and handlers. Some people would probably argue that they are interchangeable. To me however, they have slightly different meaning. A listener is an object that subscribes for events from a source. Cf. the observer pattern. Usually you can have many listeners subscribing for each type of event, and … Read more

TimerTask vs Thread.sleep vs Handler postDelayed – most accurate to call function every N milliseconds?

There are some disadvantages of using Timer It creates only single thread to execute the tasks and if a task takes too long to run, other tasks suffer. It does not handle exceptions thrown by tasks and thread just terminates, which affects other scheduled tasks and they are never run ScheduledThreadPoolExecutor deals properly with all … Read more

Stop handler.postDelayed()

You can use: Handler handler = new Handler() handler.postDelayed(new Runnable()) Or you can use: handler.removeCallbacksAndMessages(null); Docs public final void removeCallbacksAndMessages (Object token) Added in API level 1 Remove any pending posts of callbacks and sent messages whose obj is token. If token is null, all callbacks and messages will be removed. Or you could also … Read more

Where do I create and use ScheduledThreadPoolExecutor, TimerTask, or Handler?

I prefer to use ScheduledThreadPoolExecutor. Generally, if I understand your requirements correctly, all these can be implemented in your activity, TimerTask and Handler are not needed, see sample code below: public class MyActivity extends Activity { private ScheduledExecutorService scheduleTaskExecutor; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); scheduleTaskExecutor= Executors.newScheduledThreadPool(5); // This schedule a task to run every … Read more