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

CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch views

Look like you are on the wrong thread. Try using a Handler to update the GUI on the right thread. See Handling Expensive Operations in the UI Thread example from android.com. Basically you would wrap byeSetup in a Runnable and invoke it with a Handler instance. Handler refresh = new Handler(Looper.getMainLooper()); refresh.post(new Runnable() { public … Read more

Timertask or Handler

Handler is better than TimerTask. The Java TimerTask and the Android Handler both allow you to schedule delayed and repeated tasks on background threads. However, the literature overwhelmingly recommends using Handler over TimerTask in Android (see here, here, here, here, here, and here). Some of reported problems with TimerTask include: Can’t update the UI thread … Read more

Javascript event handler with parameters

I don’t understand exactly what your code is trying to do, but you can make variables available in any event handler using the advantages of function closures: function addClickHandler(elem, arg1, arg2) { elem.addEventListener(‘click’, function(e) { // in the event handler function here, you can directly refer // to arg1 and arg2 from the parent function … Read more

Create a custom event in Java

You probably want to look into the observer pattern. Here’s some sample code to get yourself started: import java.util.*; // An interface to be implemented by everyone interested in “Hello” events interface HelloListener { void someoneSaidHello(); } // Someone who says “Hello” class Initiater { private List<HelloListener> listeners = new ArrayList<HelloListener>(); public void addListener(HelloListener toAdd) … Read more

Is AsyncTask really conceptually flawed or am I just missing something?

How about something like this: class MyActivity extends Activity { Worker mWorker; static class Worker extends AsyncTask<URL, Integer, Long> { MyActivity mActivity; Worker(MyActivity activity) { mActivity = activity; } @Override protected Long doInBackground(URL… urls) { int count = urls.length; long totalSize = 0; for (int i = 0; i < count; i++) { totalSize += … Read more

Calling a function when ng-repeat has finished

var module = angular.module(‘testApp’, []) .directive(‘onFinishRender’, function ($timeout) { return { restrict: ‘A’, link: function (scope, element, attr) { if (scope.$last === true) { $timeout(function () { scope.$emit(attr.onFinishRender); }); } } } }); Notice that I didn’t use .ready() but rather wrapped it in a $timeout. $timeout makes sure it’s executed when the ng-repeated elements … Read more

jQuery .live() vs .on() method for adding a click event after loading dynamic html

If you want the click handler to work for an element that gets loaded dynamically, then you set the event handler on a parent object (that does not get loaded dynamically) and give it a selector that matches your dynamic object like this: $(‘#parent’).on(“click”, “#child”, function() {}); The event handler will be attached to the … Read more