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);
}
}