Why use related (not started) services in the application process?

As everyone knows, there are two main types of services in Android: started and tied (I do not calculate initial and related services, since they are basically the same as the services that were just started).

You can find many guides on how to use related services or how to bind to a running service, but there really is no answer to the question of why someone will use related (not started) services during the application (in other words, without IPC) ?

Is there any (hidden?) Profit from using a related service (say, for some processing) using standard thread processing tools (AsyncTaks, Executors, plain threads)? Is it worth the boilerplate code to connect such a service?

Some context

The question appeared after pumping through Google Camera sources. They create a binding (once again - not started) for saving images. What's the point? Why not just use Executor ? Did I miss something important?

If this is a related service, then there is no way to help save progress when changing the device configuration (i.e., the device is rotated). Therefore, I do not see any advantages.

+5
source share
1 answer

The initial service is useful when there is a need for the absence or very limited (one-way) interaction between the source component, that is, the activity or BroadcastReceiver, and the running service. For example, upload a background file with fire and oblivion. You provide the loader with a URL, start the service, and forget about all this. The only way the download service ever interacts with the user is to use notifications. At the same time, activity can also go back and forth, you don't care. Please note that in this case, the service serves the initial operation, and there is no need for its availability for other activities.

On the other hand, a related service is a more general service or service, which should serve several activities, and, moreover, requires several bi-directional interactions, i.e. The Activity sends a message to the service, then the Service sends the message back to the Activity, and so on. Consider the example of the background music player service, in which you transfer the music file or the URI / URL of the remote channel to the service, then another action can change the volume or switch to another track, etc. The message from the service to the activity may be that the mp3 file is incomplete or damaged, or a message with a completed track.

In fact, I came to this question looking for the answer to this exact question, but found the answer quite satisfactorily and fully in the link provided by @SagarPikhwal. Admittedly, I'm new to Android programming, so above all, I understand!

Edit: I realized that I did not answer (as much as possible) the other part of the code question that you saw for the Google camera. I think the reason they create a related service is because the camera is a shared resource, and there can be several users of this system resource at the same time. An activity that uses the camera service to capture an image or video is not an exclusive user. The Google Camera app is another user of the camera hardware, while others can work, and they are all served by a related service.

+2
source

Source: https://habr.com/ru/post/1201833/


All Articles