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.
It sounds like you want to use an
IntentServiceto implement your child thread’s functionality (it already acts like a message queue, to save you reinventing the wheel), and aBroadcastReceiverto 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
IntentServiceandBroadcastReceiverin exactly this way.