Yugandhar Babu
Yugandhar Babu

Reputation: 10349

Android Handler not receiving messages

In my android application I created 2 handlers. One handler received message and waiting(calling wait();). At this point if I post messages to second Handler, it is not receiving. After some time I am getting dialog box saying Application not responding "Force Close" or "Wait buttons".

I think because of wait() in one handler, blocking second handler to receive messages.

Please suggest me solution to this problem.

Upvotes: 1

Views: 1609

Answers (1)

nandeesh
nandeesh

Reputation: 24820

You should create two threads and create a handler in each of these two threads and call Looper.loop in the run method of both the threads

class HandlerThread1 extends Thread {
      public Handler mHandler;

      public void run() {
          Looper.prepare();

          mHandler = new Handler() {
              public void handleMessage(Message msg) {
                  // process incoming messages here
              }
          };

          Looper.loop();
      }
  }

Same way create HandlerThread2 and start both the threads.

You should never call wait in Ui thread, that is the reason you are getting App not responding popup. You can always post another message with duration instead of wait

Upvotes: 2

Related Questions