在我的 android 应用程序中,我创建了 2 个处理程序。一个处理程序收到消息并等待(调用 wait();)。此时,如果我将消息发布到第二个处理程序,则它不会接收。一段时间后,我收到一个对话框,提示应用程序未响应“强制关闭”或“等待按钮”。
我认为是因为一个处理程序中的 wait(),阻止了第二个处理程序接收消息。
请建议我解决这个问题。
最佳答案
您应该创建两个线程并在这两个线程中的每一个中创建一个处理程序,并在两个线程的 run 方法中调用 Looper.loop
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();
}
}
以同样的方式创建 HandlerThread2 并启动两个线程。
你永远不应该在 Ui 线程中调用等待,这就是你得到 App not responding popup 的原因。您始终可以发布另一 strip 有持续时间的消息,而不是等待
关于Android 处理程序不接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12986122/