操作系统版本:安卓5.1
我正在使用 android.os.Handler 在各种对象之间传递消息,它工作了几千次(有时是几百次),然后丢失了一条消息。发送成功,sendMessage() 返回 true,但从不调用 handleMessage()。应用程序继续工作,即使错过了消息,消息也会继续流动。
没有异常或崩溃。
- 难道我做错了什么?任何的意见都将会有帮助
- 它是 Android 5.1 中的已知错误吗?(我试过搜索但找不到)
我有一个应用程序,其中有 5 个单例组件使用 android 处理程序将消息发送到另一个类“xyz”。xyz 扩展了 android.os.Handler 并覆盖了 handleMessage() 函数。xyz 实例化所有 5 个单例组件,并在 xyz 构造函数中传递它们“this”(它自己的引用)。然后组件使用该引用调用 sendMessage()。
主要活动:
onCreate() {
mController = Controller.getInstance ();
}
控制器:
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_组件:
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);
}
}
USB管理器:
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. */
}
}
紫外线分量:
class UVComponent extends Handler {
UVComponent(Looper looper) {
super(looper);
}
}