gpt4 book ai didi

Android Handler 不处理消息

转载 作者:太空狗 更新时间:2023-10-29 16:03:19 25 4
gpt4 key购买 nike

我对这个很困惑。我正在服用 this Coursera course ,并致力于 Assignment 5 .我能够以最小的困难完成任务,然后继续尝试使代码达到我自己的目的。我也对其进行了一些调整,但我遇到了一个重大问题,我终生无法解决。处理程序似乎根本不接收消息...知道我做错了什么吗?

这是我的一些代码。如果您还想看什么,请告诉我。

处理程序:

public class DownloadHandler extends Handler {
private WeakReference<Context> mContext;
public DownloadHandler(Context context) {
super();
mContext = new WeakReference<Context>(context);
Log.v(TAG,"Created! "+this);
}
// Handle any messages that get sent to this Handler
@Override
public void handleMessage(Message msg) {
Log.v(TAG, "" + msg); //Never reached
...
}
}

在我继续之前,我已经通过发送一条简单消息 (handler.sendEmptyMessage(0);) 测试了处理程序,但没有成功。有什么想法吗?

Log.v(TAG,"handler="+handler);
Log.v(TAG,"Sending test "+handler.sendEmptyMessage(0));

和 logcat:

06-14 22:47:34.700: V/DownloadAddOnProducts(7406): handler=Handler (com.kd7uiy.hamfinder.DownloadHandler) {527724dc}
06-14 22:47:34.700: V/DownloadAddOnProducts(7406): Sending test true

为了进一步减少这种情况,我进行了单元测试:

public class DownloadTester extends InstrumentationTestCase {
public void setUp() {
targetContext=getInstrumentation().getTargetContext();
}

public void testDownloadHandler() {
Looper.myLooper().setMessageLogging(new LogPrinter(Log.VERBOSE,"DownloadLooper"));
DownloadHandler handler=new DownloadHandler(targetContext,Looper.myLooper());
Log.v(TAG,"handler="+handler);
handler.sendEmptyMessage(0);
Log.v(TAG,"Send empty message");
}

并且 logcat 显示:

06-14 23:03:46.068: V/DownloadHandler(8337): Created! Handler (com.kd7uiy.hamfinder.DownloadHandler) {52770be8}
06-14 23:03:46.068: V/DownloadHandler(8337): Created! Handler (com.kd7uiy.hamfinder.DownloadHandler) {52770f60}
06-14 23:03:46.068: V/DownloadTester(8337): handler=Handler (com.kd7uiy.hamfinder.DownloadHandler) {52770f60}
06-14 23:03:46.068: V/DownloadTester(8337): Send empty message

它创建了两个 DownloadHandler,因为另一个用于我的单元测试集中的另一个单元测试。

最佳答案

引用 the documentation for Handler :

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

引用 the documentation for the zero-argument Handler constructor您正在使用:

Default constructor associates this handler with the Looper for the current thread. If this thread does not have a looper, this handler won't be able to receive messages so an exception is thrown.

如果您没有崩溃,并且您没有创建自己的 HandlerThread(或等效的),那么您的 DownloadHandler 可能会将自己绑定(bind)到主应用程序线程。您发送到 DownloadHandler 的消息只会在主应用程序线程处理完其上的所有其他消息后,才会传递给 handleMessage() 方法消息队列。而且,只要您使用主应用程序线程(例如,执行 ServiceonCreate()),该消息就不会出现(并且您的 UI 将被卡住启动)。

关于Android Handler 不处理消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24224974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com