Custom Events Model without using DOM events in JavaScript

If you want to make a completely stand alone event system without relying on DOM events you can have something like this using reactor pattern function Event(name){ this.name = name; this.callbacks = []; } Event.prototype.registerCallback = function(callback){ this.callbacks.push(callback); } function Reactor(){ this.events = {}; } Reactor.prototype.registerEvent = function(eventName){ var event = new Event(eventName); this.events[eventName] = … 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

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

How to create a Looper thread, then send it a message immediately?

Eventual solution (minus error checking), thanks to CommonsWare: class Worker extends HandlerThread { // … public synchronized void waitUntilReady() { d_handler = new Handler(getLooper(), d_messageHandler); } } And from the main thread: Worker worker = new Worker(); worker.start(); worker.waitUntilReady(); // <- ADDED worker.handler.sendMessage(…); This works thanks to the semantics of HandlerThread.getLooper() which blocks until the … 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

How do I write an Ansible handler with multiple tasks?

There is proper solution to this problem as of Ansible 2.2. handlers can also “listen” to generic topics, and tasks can notify those topics as follows: handlers: – name: restart memcached service: name=memcached state=restarted listen: “restart web services” – name: restart apache service: name=apache state=restarted listen: “restart web services” tasks: – name: restart everything command: … Read more

Accessing UI thread handler from a service

This snippet of code constructs a Handler associated with the main (UI) thread: Handler handler = new Handler(Looper.getMainLooper()); You can then post stuff for execution in the main (UI) thread like so: handler.post(runnable_to_call_from_main_thread); If the handler itself is created from the main (UI) thread the argument can be omitted for brevity: Handler handler = new … Read more

What is a handler? [closed]

A handler is a routine/function/method which is specialized in a certain type of data or focused on certain special tasks. Examples: Event handler – Receives and digests events and signals from the surrounding system (e.g. OS or GUI). Memory handler – Performs certain special tasks on memory. File input handler – A function receiving file … Read more