【发布时间】:2014-09-25 11:44:51
【问题描述】:
我想从 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");
}
}
}
找到的最佳示例:
- HandlerThread Test
- How to create a Looper thread, then send it a message immediately?
- Async calls with Handler
- HandlerThread vs Executor - When is one more appropriate over the other?
- Best use of HandlerThread over other similar classes
- Android HandlerThread
- HandlerThread examples
- Android: Passing data between main and worker threads
- Java Synchronised
- Sending messages between threads using activity thread queue and Handler class
- Intro to Loopers and Handlers
- developer.android: Specifying the Code to Run on a Thread
至少现在我可以关闭该死的标签
解决方案由 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");
}
}
}
【问题讨论】:
-
认为您可以使用 github.com/greenrobot/EventBus 用 4 行代码做到这一点
-
非常有趣,谢谢,下次再研究。 +1。但我认为我应该在跑步之前先走路
-
没问题,但说实话,尽管我已经编写了很多 Android 代码,但我还是放弃了使用 Handlers 和 Messages。当它可以做得非常简单,可读和可维护的IMO时,不需要做复杂的事情:)
-
上帝知道,我知道你来自哪里!!!!有人告诉我这将是一个赏金问题,哈哈
-
1) 创建一个新的 HandlerThread,2) start() 它,3) 创建一个新的 Handler。使用 ht.getLooper() 作为 Looper 参数,4) 发送消息
标签: android multithreading android-handler android-handlerthread