14

I have a main activity and bound service. The bound service receives a command from the network, which triggers a Message to be sent to a Handler in the main activity. I got everything to work by passing the Handler's reference to the service. And then... I stumbled across this thing called a Messenger.

Messenger: Reference to a Handler, which others can use to send messages to it. This allows for the implementation of message-based communication across processes, by creating a Messenger pointing to a Handler in one process, and handing that Messenger to another process.

This inspired a few questions:

  • Does the term process mean I won't need to use a Messenger unless I generate a separate process under the Process and Thread guidelines?
  • Is it always good practice to use a Messenger?
  • Is passing a reference to the Handler typically a bad idea?
Rich
  • 785
  • 1
  • 9
  • 22
  • This is not an answer for your question, but I suggest that you should use AsyncTask for background threads. (see http://developer.android.com/reference/android/os/AsyncTask.html) – Nguyen Minh Binh Jul 30 '13 at 07:09
  • 1
    I'm using a bound IntentService, which should handle the asynchronous requests. I don't think an AsyncTask is appropriate, because the documentation states _"AsyncTasks should ideally be used for short operations (a few seconds at the most.)"_, while my service is listening for commands from the Internet and may run for the entire lifetime of the activity. – Rich Jul 30 '13 at 19:03
  • 1
    Also as a side comment, AsyncTasks must be invoked from the UI thread – Selecsosi Jul 30 '13 at 20:03

2 Answers2

10

Does the term process mean I won't need to use a Messenger unless I generate a separate process under the Process and Thread guidelines?

This is correct. You need a Messenger if you want to bind a remote service (e.g. running in another process). In this case, the only option to provide remote service with a pointer to your Handler would be to use a Messenger.

Is it always good practice to use a Messenger?

I would not say so. Use it when it's really needed - for remote services or if you start service without binding it. You can pack Messenger as an extra into an Intent and start service with it. Thus you can pass reference to a Handler without binding a service. This is another case when Messenger is useful.

Is passing a reference to the Handler typically a bad idea?

If you properly handle registration and unregistration of Handler when activity starts and stops, then you should be ok. There is no need to use Messenger for local service then. If you don't unregister handler properly, there will be memory leaks (service refers a handler, and handler refers a stopped activity).

sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86
1

The main advantage of Messenger over handler is, Messenger is able to queue all the messages sent from various clients. While Handler needs a looper to do that. So messenger gives an extra edge when used in IPC among remote processes

Gopal S Akshintala
  • 1,933
  • 1
  • 17
  • 24