Reputation: 117597
The Handler in Android is used to send messages between classes. For example:
public class Foo
{
private Handler handler;
public Foo(Handler handler)
{
this.handler = handler;
// ...
}
public void fooMethod()
{
// ...
handler.obtainMessage("blah blah").sendToTarget();
}
}
Main:
public class Main
{
private Handler handler;
public Main()
{
handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
// handle the message that is sent from Foo
}
};
Foo f = new Foo(handler);
// ...
}
}
So, I am looking for a similar technique in Java, or should I implement my own handler?
Upvotes: 9
Views: 4056
Reputation: 1309
There isn't one by default. But you can make your own custom class that does a similar thing. I made one like this:
import java.util.ArrayList;
public class Handler extends Object{
ArrayList<Message> messages;
public Handler(){
messages = new ArrayList<>();
}
public final Message obtainMessage(int what, Object obj){
Message message = new Message(what, obj, this);
messages.add(message);
return message;
}
public final Message obtainMessage(int what){
Message message = new Message(what, this);
messages.add(message);
return message;
}
public void handleMessage(Message msg){
messages.remove(msg);
}
public final boolean hasMessages(){
return !messages.isEmpty();
}
}
you would then also need a custom Message class like this:
public class Message extends Object{
int mWhat;
Object mObj;
Handler mTarget;
public Message(int what, Object obj){
mWhat = what;
mObj = obj;
}
public Message(int what, Object obj, Handler target){
mWhat = what;
mObj = obj;
mTarget = target;
}
public Message(int what, Handler target){
mWhat = what;
mTarget = target;
}
public Message(int what){
mWhat = what;
}
public void sendToTarget(){
mTarget.handleMessage(this);
}
}
You can use this Handler to communicate from a background thread to the UI thread, without disturbing the UI thread too much.
You can use this class in completely the same way as you would in Android: First you create an instance in your UI class:
final int MESSAGE_RECEIVED = 0;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.mWhat){
case MESSAGE_RECEIVED:
System.out.println("Message received.");
updateStatus("Message received.");
break;
}
}
};
Then you supply this Handler instance to your background thread:
Thread thread = new T1(handler);
thread.start();
And last, you send messages by the means of:
mHandler.obtainMessage(0).sendToTarget();
I tested this method on a sample applet program i did, and it seems to work perfectly. Although I'm not an expert java programmer, so there might be some downsides to this, that I'm not really aware of. If so, I would love to hear an educated explanation of why this isn't ok.
Hope this helps someone.
NOTE: The above Handler and Message classes (my code) do not provide the full functionality of Android.Handler and Android.Message.
Upvotes: 3
Reputation: 66
I believe that you are looking for something like Observer Pattern, that makes easy the communication of an object to another.
See >>> http://www.javaworld.com/javaqa/2001-05/04-qa-0525-observer.html
Also >>> http://www.oodesign.com/observer-pattern.html
I hope I've helped...
Upvotes: 3