0

I have never worked with services before, but I looked at many posts and tutorials and I never quite understood how this works.

So what I need is something (like a Service) that runs in the background independently of the apps lifecycle to receive and execute requests asynchronically. In more detail I want this to be a download queue. I planned on doing this by having a service started if the user requests a download. If another download is requested while the first isn't finished the service puts it into its queue. The service sends periodical messages so the UI gets updated. The user can also pause or cancel a download (so the activity has to have the ability to send messages to the service).

I know how to do the download stuff and I think I know how to start the Service, but I don't understand how to send messages to the service.
For messages to the Activity I would do something like this:

private void sendUpdateMessage(Bundle data) {
    Intent intent = new Intent("DownloadUpdate");
    intent.putExtra("data", data);
    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}

For messages to the Service I guess there has to be some sort of binding (which I don't understand well). I found this Communication between Activity and Service or this https://developer.android.com/reference/android/app/Service.html#remote-messenger-service-sample, but this seems very complicated and I sadly do not understand it.

It would be nice if anyone had a simple explenation for how to communicate between Activity and Service.
(Again, I know there are post on this topic on Stackoverflow, but for some reason I do not understand how to do it properly)

JojoIV
  • 992
  • 1
  • 9
  • 12

1 Answers1

0

I found a very helpful page with great examples:
Effective communication between Service and Activity

There are three possible ways:

  • Broadcasts and BroadcastReceivers

    The simplest solution uses Broadcasts and BroadcastReceivers. However, this method has the following disadvantages:
    The message sender does not know who will receive the data (it broadcasts the message). Broadcasts consume a lot of bandwidth and are not suited for high traffic.

  • Binder

    For Activities and Services used in one application the “Binder” solution is recommended. After binding with the service, the Binder will allow the activity (or other class bound to the service) to call any public method of the service.

  • Messenger

    The recommended method of communication between Activities and Services for different applications’ Inter-Process Communication (IPC) is by using Messengers. In this case, the service defines a handler for different incoming messages. This handler is then used in Messenger for transferring data via messages.
    To get bi-directional communication, the activity also has to register a Messenger and Handler. This Messenger is passed to the service in one of messages. Without this the service wouldn’t know to who it should respond.

For my project I chose the last one.

JojoIV
  • 992
  • 1
  • 9
  • 12