在android系统消息队列 [英] Message queue in android
本文介绍了在android系统消息队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有谁能够告诉我,
怎样才能从消息队列中的数据(消息)?
或如何从主线程发送消息给其他线程?
can anybody tell me, how can get data(message) from message queue ? or how can send message from main thread to other thread ?.
感谢
推荐答案
如果你想你应该运行的Looper 并创建绑定到此尺蠖的消息处理器 。在UI线程默认有一个活套。还有用于创建与勾针称为 HandlerThread 线程便捷类。下面是关于处理程序和活套的好文章: Android的胆量:简介活套和处理程序。
If you want to receive messages on a thread you should run a Looper and create a message Handler bound to this looper. The UI thread has a looper by default. There's a convenient class for creating threads with loopers called HandlerThread. Here's a good article about Handlers and Loopers: Android Guts: Intro to Loopers and Handlers.
修改
HandlerThread thread = new HandlerThread("Thread name");
thread.start();
Looper looper = thread.getLooper();
Handler handler = new Handler(looper) {
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case SOME_MESSAGE_ID:
// SOME_MESSAGE_ID is any int value
// do something
break;
// other cases
}
}
};
handler.post(new Runnable() {
@Override
public void run() {
// this code will be executed on the created thread
}
});
// Handler.handleMessage() will be executed on the created thread
// after the previous Runnable is finished
handler.sendEmptyMessage(SOME_MESSAGE_ID);
这篇关于在android系统消息队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文