0

I'm writing a simple socket client app for android, here is my handler class:

static Handler IncomingHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if (flag) {
            kq += msg.obj.toString() + "\r\n";
            textResponse.setText(kq);
        }
    }
};

This handler has a "should be static or leaks may occur" warning, and it did. It keeps running in a loop, using 40% CPU.

According to this question, I changed my handler class to

static class IncomingHandler extends Handler {
    private final WeakReference<MainActivity> mService; 

    IncomingHandler(MainActivity service) {
        mService = new WeakReference<MainActivity>(service);
    }
    @Override
    public void handleMessage(Message msg)
    {
         MainActivity service = mService.get();
         if (service != null) {
          if (flag) {
                kq += msg.obj.toString() + "\r\n";
                textResponse.setText(kq);
            }
         }
    }
}

It doesn't have "leaks may occur" warning anymore, but now a new problem appeared: I can't refer IncomingHandler from another class

public class ClientTask extends AsyncTask<String, String, String> implements
        OnListener {
////Some random code
    void sendMessage(String msg) {
        try {
                if (!msg.equals("bye"))
                MainActivity.IncomingHandler.obtainMessage(0, 0, -1, "Me: " + msg)
                        .sendToTarget();
            else
                MainActivity.IncomingHandler.obtainMessage(0, 0, -1,
                        "Disconnecting!").sendToTarget();
        } catch (Exception ioException) {
            ioException.printStackTrace();
        }
    }

Now it says "non-static method obtainMessage cannot be referenced from a static context”. But making IncomingHandler non-static makes the whole thing pointless. Is there anyway I can use obtainMessage from a static context?

EDIT1: I tried to add static IncomingHandler incoming = new IncomingHandler(MainActivity.this); and used MainActivity.incoming.obtainMessage(); but it said "MainActivity.this cannot be referenced from a static context" so commenters' suggestions don't work for me.

EDIT2: I was dumb, I forgot to remove "MainActivity" from MainActivity.incoming.obtainMessage(). The problem is still there though.

Community
  • 1
  • 1
ProudNoob
  • 77
  • 2
  • 13
  • 1
    I believe `obtainMessage` is not a static method, meaning you should create an instance of `MainActiviey.InComingHandler` and call `obtainMessage` on that object – 0xDEADC0DE Sep 15 '16 at 08:41
  • 1
    You may be confusing the static nested class (meaning the handler isn't attached to a particular instance of the activity) with a static method (meaning you aren't working with any instance of the handler at all). – chrylis -cautiouslyoptimistic- Sep 15 '16 at 08:43
  • I created `static IncomingHandler incoming = new IncomingHandler(MainActivity.this);` and use `MainActivity.incoming.obtainMessage()` but they still show "MainActivity.this cannot be referenced from a static context" so i thought it wasn't the correct answer. If I change it to non-static then the obtainMessage method throws the same error. – ProudNoob Sep 15 '16 at 08:53
  • 1
    Try `IncomingHandler incoming = new IncomingHandler(this);` and `incoming.obtainMessage()`. – K Neeraj Lal Sep 15 '16 at 09:17
  • 1
    I'd suggest you learn about Java to understand what it means to have a static field and a static class and what restrictions that implies. – Henry Sep 15 '16 at 09:33
  • @KNeerajLal can you make an answer with your comment? I will choose it as accepted answer. – ProudNoob Sep 15 '16 at 09:48
  • @ProudNoob Check the answer. – K Neeraj Lal Sep 15 '16 at 10:25

1 Answers1

1

Create an object of IncomingHandler and call obtainMessage() on that object.

Try this code,

IncomingHandler incoming = new IncomingHandler(this);
incoming.obtainMessage();
K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33