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.