我知道这是一篇很长的文章,但请耐心等待 :)
目前我正在使用 Handler 类从子线程到主 UI 线程进行通信,并且我正在使用消息队列的实现来处理消息其他方向(从主线程到子线程)。
子节点知道向哪里发送消息的方式是:在主线程中,创建Handler对象后,通过自己的消息队列将其引用发送给子节点,然后子线程存储该引用值,从而知道如何将消息发送到活动。
但是,我的应用程序中有多个活动,每个活动都需要与同一个子线程进行通信。我在一个 Activity 中创建的 Handler 对象在下一个 Activity 中无效,所以我所做的是每次创建新 Activity 时(每次用户在 Activity 之间更改时)、每次创建 Handler 对象时,与之前完全相同的方式并将其引用发送给子线程。
我的问题是:
这是正确的做法吗?
有更简单的方法吗?从子节点到多个 Activity 的通信?除了使用例如单例类或其他东西,所以我不会每次都通过自定义队列发送引用,而是更新单例中的变量。
编辑
这是根据要求的一些代码:
在每个 Activity 的 onCreate 方法中,我执行以下操作:
...
//Create the main handler on the main thread so it is bound to the main thread's message queue.
createHandler();
//Send to the controller the msg handler of the UI thread
//Create messenger of appropriate type
Messenger mess = new Messenger(_MSGHANDLER_);
//Add the handler
mess.addContent(_HANDLERTAG_, mMainHandler);
//Add the name of this activity
mess.addContent(_ACTIVITYNAMETAG_, "RemyLoginActivity");
//Add the message to the controller's queue
controller.enqueue(mess)
...
函数 createHandler 创建处理程序对象并附加特定的回调函数。
//Create the handler on the main thread so it is bound to the main thread's message queue
private void createHandler()
{
//Create the main handler on the main thread so it is bound to the main thread's message queue.
mMainHandler = new Handler()
{
public void handleMessage(Message msg)
{
//Get the messenger from the message
Messenger mess = (Messenger)msg.obj;
Log.i( "Remy", "ActivityMainMenu::Message from Controller: " + mess.type());
switch( mess.type() )
{
default: Log.i("Remy","OUPS::createHandler::Could not understand queue message type: " + mess.type());
break;
}
}
};
}
最后,控制器是子线程,当它接收到处理程序引用时,它只是存储它,然后发送如下消息:
activityHandler.sendMessage( msg );
希望我没有忘记什么。
I know this is a long post, but please bear with me :)
Currently i'm using the Handler class to communicate from a child thread to the main UI thread, and i'm using my implementation of a message queue for messages in the other direction (from main thread to child thread).
The way the child node knows where to send the messages is: in the main thread, after i create the Handler object, i send it's reference to the child through my own message queue, the child thread then stores the reference value and thus will know how to send the messages to the activity.
BUT, i have multiple Activities in my application, and each one needs to communicate with the same child thread. The Handler object I create in one Activity won't be valid in the next Activity, so what I've done is every time a new activity is created, (every time the user changes between activities), every time i create a Handler object, exactly the same way as before and send it's reference to the child thread.
MY QUESTIONS ARE:
Is this a proper way of doing it?
Is there an easier way of doing this? Of communicating from a child node to multiple Activities? Except using for example a singleton class or something, so i don't send the reference every time through my custom queue but instead update the variable in the singleton.
EDIT
Here is some code, as requested:
In the onCreate method of every Activity i do the following:
...
//Create the main handler on the main thread so it is bound to the main thread's message queue.
createHandler();
//Send to the controller the msg handler of the UI thread
//Create messenger of appropriate type
Messenger mess = new Messenger(_MSGHANDLER_);
//Add the handler
mess.addContent(_HANDLERTAG_, mMainHandler);
//Add the name of this activity
mess.addContent(_ACTIVITYNAMETAG_, "RemyLoginActivity");
//Add the message to the controller's queue
controller.enqueue(mess)
...
The function createHandler creates the handler object and attaches the specific callback function.
//Create the handler on the main thread so it is bound to the main thread's message queue
private void createHandler()
{
//Create the main handler on the main thread so it is bound to the main thread's message queue.
mMainHandler = new Handler()
{
public void handleMessage(Message msg)
{
//Get the messenger from the message
Messenger mess = (Messenger)msg.obj;
Log.i( "Remy", "ActivityMainMenu::Message from Controller: " + mess.type());
switch( mess.type() )
{
default: Log.i("Remy","OUPS::createHandler::Could not understand queue message type: " + mess.type());
break;
}
}
};
}
and finally , the controller is the child thread in which when it receives the handler reference it just stores it, and then it sends messages like so:
activityHandler.sendMessage( msg );
Hope i haven't forgotten something.
发布评论
评论(1)
听起来您想使用
IntentService
来实现子线程的功能(它已经充当消息队列,以节省您重新发明轮子),并使用BroadcastReceiver
来实现允许孩子向任何感兴趣的各方广播其结果(在您的情况下将是任何相关活动)。请参阅本教程,了解使用
的良好示例IntentService
和BroadcastReceiver
正是这种方式。It sounds like you want to use an
IntentService
to implement your child thread's functionality (it already acts like a message queue, to save you reinventing the wheel), and aBroadcastReceiver
to allow the child to broadcast its results to any interested party (which in your case will be any of the relevant activities).See this tutorial for a good example of using
IntentService
andBroadcastReceiver
in exactly this way.