我像这样关闭线程:
theSensor.getLooper()。quit();
theSensor.quit();
仍然我得到下面的错误。我需要怎么做才能忽略其余消息?
03-14 00:42:12.237: W/MessageQueue(10027): Handler (android.os.Handler) {41d5e6d8} sending message to a Handler on a dead thread
03-14 00:42:12.237: W/MessageQueue(10027): java.lang.RuntimeException: Handler (android.os.Handler) {41d5e6d8} sending message to a Handler on a dead thread
03-14 00:42:12.237: W/MessageQueue(10027): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
03-14 00:42:12.237: W/MessageQueue(10027): at android.os.Handler.enqueueMessage(Handler.java:618)
03-14 00:42:12.237: W/MessageQueue(10027): at android.os.Handler.sendMessageAtTime(Handler.java:587)
03-14 00:42:12.237: W/MessageQueue(10027): at android.os.Handler.sendMessageDelayed(Handler.java:558)
03-14 00:42:12.237: W/MessageQueue(10027): at android.os.Handler.sendMessage(Handler.java:495)
03-14 00:42:12.237: W/MessageQueue(10027): at android.os.Message.sendToTarget(Message.java:360)
03-14 00:42:12.237: W/MessageQueue(10027): at com.sj.framework.tiltsensor.TiltSensor.onSensorChanged(TiltSensor.java:158)
03-14 00:42:12.237: W/MessageQueue(10027): at android.hardware.SystemSensorManager$ListenerDelegate$1.handleMessage(SystemSensorManager.java:256)
03-14 00:42:12.237: W/MessageQueue(10027): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 00:42:12.237: W/MessageQueue(10027): at android.os.Looper.loop(Looper.java:137)
03-14 00:42:12.237: W/MessageQueue(10027): at android.app.ActivityThread.main(ActivityThread.java:5306)
03-14 00:42:12.237: W/MessageQueue(10027): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 00:42:12.237: W/MessageQueue(10027): at java.lang.reflect.Method.invoke(Method.java:511)
03-14 00:42:12.237: W/MessageQueue(10027): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
03-14 00:42:12.237: W/MessageQueue(10027): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
03-14 00:42:12.237: W/MessageQueue(10027): at dalvik.system.NativeStart.main(Native Method)
最佳答案
您是否尝试过quitSafely方法? api文档中的描述看起来像您所想要的。它将禁用MessageQueue,以便sendMessage将返回false,并且尝试传递消息将不会成功。 quitSafely
关于java - Android:正确关闭线程和Looper吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22393604/