Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any class in Java similar to android.os.Handler in Android?

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?

like image 696
Eng.Fouad Avatar asked Mar 04 '12 21:03

Eng.Fouad


People also ask

What can we use instead of handler in Android?

Use Executor instead of handler for more info Executor.

What is a handler class in Java?

In android Handler is mainly used to update the main thread from background thread or other than main thread. There are two methods are in handler. Post() − it going to post message from background thread to main thread using looper.

Is handler deprecated in Android?

Handler() and Handler(Handler. Callback callback) constructors are deprecated. Because those can leads to bugs & crashes. Use Executor or Looper explicitly.

What is looper in Java?

Looper is a class that runs message loop for some thread. In Java we have threads, in which we might do some useful work. Initially threads don't have an event loop capabilities, but we can add this capabilities via attaching Looper to them. Looper has two main methods: prepare and loop .


2 Answers

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...

like image 109
vitorvellozo Avatar answered Sep 30 '22 18:09

vitorvellozo


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.

like image 28
ak93 Avatar answered Sep 30 '22 17:09

ak93