This is my first StackOverflow question, so I apologize for any ambiguity if there’s any.
I am looking for a way to receive a callback from an Android handler.
For example, if the handler is called from within the activity, what’s the proper way to receive a callback from it which let’s say gonna modify some text on the screen inside that activity.
Thanks!
-Yevgeniy
public class MyHandler extends Handler{
@Override
public void handleMessage (Message msg) {
//Do something
// ???send back the result to the called activity???
}
}
public class MyActivity extends Activity{
public void callHandler(){
MyHandler handler = new MyHandler();
handler.sendEmptyMessage( 0 );
}
}
At this point, from what you’ve shown, there isn’t a lot of point in having a Handler at all. A Handler is a mechanism that is attached to the thread that creates it, soley for the purpose of scheduling tasks in that same thread, so if callHandler is called by another thread, The handler will run in whatever the thread that called callHandler was. The model I’ve found the most success with for Android IPC is like this
What this does, in brief, is starts an asynchronous task…. when that task is finished, it calls updateUI. updateUI puts code to be executed into the UI thread so that views can be modified, etc. In this way, as far as your asynchronous task is concerned, all it has to do is call a method to update the UI. In reality, things are a bit more complicated, but it keeps your activity’s interface clean and easy to use for any asynchronous task.