我想从GUI线程设置HandlerThread。然后一段时间后,当在GUI上单击一个按钮时,它将运行callHello(),然后将消息发送到驻留在非GUI线程上的HelloLogger对象,该对象异步记录“ Hello World”。我已经尝试了很多方法,有的无限期地阻塞,有的从未收到消息,等等。下面的代码与我所获得的代码差不多,请有人对其进行修改以使其工作吗?public class HandlerThreadExample { private MyHandlerThread mMyHandlerThread; private Looper mLooper; private Handler mHandler; public HandlerThreadExample(){ mMyHandlerThread = new MyHandlerThread(); mMyHandlerThread.start(); mLooper = mMyHandlerThread.getLooper(); } public void callHello() { mHandler.sendEmptyMessage(1); } private class MyHandlerThread extends HandlerThread { private HelloLogger mHelloLogger; private Handler mHandler; public MyHandlerThread() { super("The MyHandlerThread thread", HandlerThread.NORM_PRIORITY); } public void run (){ mHelloLogger = new HelloLogger(); mHandler = new Handler(getLooper()){ public void handleMessage(Message msg){ mHelloLogger.logHello(); } }; super.run(); } } private class HelloLogger { public HelloLogger (){ } public void logHello(){ Log.d("HandlerThreadExample", "Hello World"); } }}
3 回答
- 3 回答
- 0 关注
- 409 浏览
添加回答
举报
0/150
提交
取消