0

OS Version: Android 5.1

I am passing messages between various objects using android.os.Handler and it works for a few thousand times (sometimes a few hundred) and then a message is missed. It is sent successfully, sendMessage() returns true, but handleMessage() is never called. App continue to work, and message keep flowing even after the missed message.

There are no exceptions or crashes.

  • Am I doing something wrong? Any suggestions would be helpful
  • Is it a known bug in Android 5.1? (I've tried to search but couldn't find one)

I have an application where there are 5 singleton components sending messages to another class "xyz" using android Handler. xyz extends android.os.Handler and overrides handleMessage() function. xyz instantiates all 5 singleton components and passing them 'this' (its own reference) in the xyz constructor. The components then use that reference to call sendMessage().

MainActivity:

onCreate() {
    mController = Controller.getInstance ();
}

Controller:

Controller() {
    /* Following is the USB Manager. */
    mUsbHandlerThread = new HandlerThread("USBManagerHandler", Thread.NORM_PRIORITY);
    mUsbManagerHandlerThread.start();
    mUSBManager = USBManager.getInstance(mUsbManagerHandlerThread.getLooper());

    /* Following are the components which send messages to USB Manager. */
    mBmsComponentHandlerThread = new HandlerThread("BMSComponentManagerHandler", Thread.NORM_PRIORITY);
    mBmsComponentHandlerThread.start();
    mBmsComponent = BMSComponent.getInstance(mBmsComponentHandlerThread.getLooper());
mBmsComponent.setUSBManagerHanlder(mUSBManager)
    /* There are 4 other components initialized the same way. */

}

BMS_Component:

public class BMSComponent extends UVComponent {

    public Handler usbHandler;

    private BMSComponent (Looper looper) {
        super(looper);
    }

    public setUSBHandler(USBComponent usbHandler) {
        this.usbHandler = usbHandler;
    }

    public postToUSBManager(Message message) {
        this.usbHandler.sendMessage(message);
    }

}

USBManager:

public class USBManager extends UVComponent {

    public Handler usbHandler;

    private USBManager (Looper looper) {
        super(looper);
    }

    @Override
    public void handleMessage(Message msg) {
        /* process the message here, and call the response handler which there in the msg object. */

}

    }

UVComponent:

class UVComponent extends Handler {

    UVComponent(Looper looper) {
        super(looper);

    }

}
qureshi
  • 1
  • 2
  • i can't tell what is wrong just by the description. If you posted the code, we could inspect it – nandsito Apr 10 '17 at 09:24
  • @nandsito The overall code is too big. I've eliminated the unrelated parts of the code but added the handler related code to the main question. Thanks for helping! – qureshi Apr 10 '17 at 13:49
  • is `USBManager` class actually what's shown in the code snippet? – nandsito Apr 10 '17 at 15:44
  • USBManager class handles the Accessory attached over USB. It has sending and receiving threads which communicate with the accessory. It has modbus implementation to communicate over the serial connection. But for handling incoming messages, there is just this 1 handleMessage method, which gets messages from components and from the receiver thread. – qureshi Apr 11 '17 at 05:12
  • The incorrect constructor name was a typo. The code is functioning 100s of times before missing a single message. – qureshi Apr 11 '17 at 07:50
  • in `USBManager.handleMessage()` you say you receive a response handler. Is it a `Handler`? and are you certain that the problem is in the code you posted, or you also consider the issue may be somewhere else, for example in the threads the `USBManager` manages? because i don't think Android would miss a Handler message just like this – nandsito Apr 11 '17 at 09:32
  • Ok, so I will focus on the other threads and code then. – qureshi Apr 12 '17 at 08:31
  • My confusion here is that there is no way to remove a message from a MessageQueue once it has been added (unless the looper quits without processing it), and the sendMessage() returns true every time in my case. Return value is true even for the missing message. – qureshi Apr 12 '17 at 08:54
  • you can *try* to remove a message from a queue, but you'll never know if the `removeMessages` call actually removed anything – nandsito Apr 12 '17 at 09:13

0 Answers0