与HandlerThread通信的示例

我想从GUI线程设置一个HandlerThread。 然后一段时间后,当在GUI上点击一个button时,它运行callHello(),然后发送一个消息给驻留在asynchronouslogging“Hello World”的非GUI线程上的HelloLogger对象。 我已经尝试了一些东西,一些块无限期地,一些永远不会收到消息,等等等等。下面的代码是或多或less如我所知,请有人可以修改它的工作?

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"); } } } 

最好的例子发现:

  • HandlerThreadtesting
  • 如何创buildLooper线程,然后立即发送消息?
  • asynchronous调用处理程序
  • HandlerThread vs Executor – 什么时候比另一个更合适?
  • 最好使用HandlerThread而不是其他类似的类
  • Android HandlerThread
  • HandlerThread的例子
  • Android:在主线程和工作线程之间传递数据
  • Java同步
  • 使用活动线程队列和Handler类在线程之间发送消息
  • 介绍活页夹和处理程序
  • developer.android:指定要在线程上运行的代码

至less现在我可以closures该死的标签

解决scheme来自pskink的帮助

 public class HandlerThreadExample2 { private static int MSG_START_HELLO = 0; private static int MSG_HELLO_COMPLETE = 1; private HandlerThread ht; private Handler mHtHandler; private Handler mUiHandler; private boolean helloReady = false; public HandlerThreadExample2(){ ht = new HandlerThread("The new thread"); ht.start(); Log.d(App.TAG, "UI: handler thread started"); mUiHandler = new Handler(){ public void handleMessage(Message msg){ if (msg.what == MSG_HELLO_COMPLETE){ Log.d(App.TAG, "UI Thread: received notification of sleep completed "); helloReady = true; } } }; mHtHandler = new Handler(ht.getLooper()){ public void handleMessage (Message msg){ if (msg.what == MSG_START_HELLO){ Log.d(App.TAG, "handleMessage " + msg.what + " in " + Thread.currentThread() + " now sleeping"); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } Log.d(App.TAG, "Woke up, notifying UI thread..."); mUiHandler.sendEmptyMessage(MSG_HELLO_COMPLETE); } } }; } public void sendLongHello(){ if (helloReady){ Log.d(App.TAG, "sending hello " + Thread.currentThread()); mHtHandler.sendEmptyMessage(MSG_START_HELLO); helloReady = false; } else { Log.e(App.TAG, "Cannot do hello yet - not ready"); } } } 

这是一个工作的例子:

 HandlerThread ht = new HandlerThread("MySuperAwesomeHandlerThread"); ht.start(); Handler h = new Handler(ht.getLooper()) { public void handleMessage(Message msg) { Log.d(TAG, "handleMessage " + msg.what + " in " + Thread.currentThread()); }; }; for (int i = 0; i < 5; i++) { Log.d(TAG, "sending " + i + " in " + Thread.currentThread()); h.sendEmptyMessageDelayed(i, 3000 + i * 1000); } 

更新

创build两个类字段:

 Handler mHtHandler; Handler mUiHandler; 

并尝试这一点:

 HandlerThread ht = new HandlerThread("MySuperAwsomeHandlerThread"); ht.start(); Callback callback = new Callback() { @Override public boolean handleMessage(Message msg) { if (msg.what == 0) { Log.d(TAG, "got a meaasage in " + Thread.currentThread() + ", now sleeping... "); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } Log.d(TAG, "woke up, notifying ui thread..."); mUiHandler.sendEmptyMessage(1); } else if (msg.what == 1) { Log.d(TAG, "got a notification in " + Thread.currentThread()); } return false; } }; mHtHandler = new Handler(ht.getLooper(), callback); mUiHandler = new Handler(callback); mHtHandler.sendEmptyMessageDelayed(0, 3000); 

你当然可以摆脱Callback界面,并用重写的handleMessage方法创build两个Handlers …

你看到的问题是因为你的外部类正在使用一个私有的mHandler字段,你的HandlerThread也是如此。 外部类'字段不被初始化。 你不需要内部的mHandler。 在调用start()之后,外层类可以从你刚才抓住的钩子中创build一个处理函数。