Android Service Startservice() and Bindservice()

When is it smart to use bindService and startService

You usually use bindService() if your calling component(Activity) will need to communicate with the Service that you are starting, through the ServiceConnection. If you do not want to communicate with the Service you can use just startService(). You Can see below diffrence between service and bind service.

From the docs :

Started

A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.

Bound

A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.

You can read more here : Android Services, Bound Services

Why is a service launched with both startService and bindService in Android Studio?

Here is some description why startService and bindService could be used together:

https://developer.android.com/guide/components/bound-services#bind-started-service

It says:

As discussed in the Services document, you can create a service that is both started and bound. That is, you can start a service by calling startService(), which allows the service to run indefinitely, and you can also allow a client to bind to the service by calling bindService().

After that:

Although you usually implement either onBind() or onStartCommand(), it's sometimes necessary to implement both. For example, a music player might find it useful to allow its service to run indefinitely and also provide binding. This way, an activity can start the service to play some music and the music continues to play even if the user leaves the application. Then, when the user returns to the application, the activity can bind to the service to regain control of playback.

android service startService() and bindService()

I think hara's answer was a little confusing. What you describe is perfectly legitimate and in fact the only way to get the behavior you want. If you create a Service by binding to it, it will die when you unbind. So the only way to keep it around without activities binding to it is to start it with startService(). There is no conflict with lifecycles because it only applies to how the service is STARTED. So once it's started with startService(), it follows that lifecycle process. So you are free to bind and unbind to it as much as you wish and it will only die when you call stopService() or stopSelf().

How can I start and bind to a service in Android?

What you're trying to do is to bind to the service first and then start it. Documentation says:

A bound service is one that allows application components to bind to it by calling bindService() in order to create a long-standing connection (and generally does not allow components to start it by calling startService()).

The lifecycle of a service created by calling bindService() has different callback methods compare to the one created by calling startService() (see the flow diagram on the page provided). So for the service created with a bindService() call there is no onStartCommand() method, that's why it's never called.

The common practice is to start a service first and after that bind to it (all the hook methods must be appropriately implemented in this case). If you do it this way onStartCommand() will be called.

EDIT:

How can I start the service?

You did start the service. After calling bindService() it is running (of course if onBind() is properly implemented with IBinder returned).

... but the onStartCommand newer appeared in the log

It is so due to the reason described above.

Android Service starting 2 times because of BindService and StartService

You don't have an unbindService call anywhere in your code. So whenever the Activity gets destroyed, the system detects that it is still bound to a ServiceConnection and has been leaked. This is still the case when calling bindService inside of a Fragment. Since fragments don't inherit from Activity or Context, they don't have a context reference themselves thus they must use their parent Activities context. Remember to always call unbindService when the owning component is being destroyed, whether it's a Fragment, Activity, or even another Service. It's not unheard for a service to bind to another.

If you don't want your bound service to be destroyed when all clients unbind, you need to add special logic to determine if the Service should transition to a started service temporarily so it won't be killed by the OS, and stop the service when a client rebinds to it.

Android Service, do we have to use startService or BindService?

However, I'm wondering what's the point of doing this?

To have the service be running, to tell the OS "hey, we are doing work here on behalf of the user, please let my process live a bit longer".

This is covered in the documentation.

Do we have to send intents to make it work?

Yes.

If I just retrieve an static instance of Service, and directly call its methods, I can also implement both start it and communicate with it.

Then it is merely a Java object, and there is no point in inheriting from Service. Also, it means that your process will live for less time when your UI is not in the foreground.

I found an example of not using startService nor bindService, but it works well as a simple MusicPlayer.

The service is started, via startService(), in ArtistSearchActivity. Until this is done, the service does not exist, and the singleton will be null.

Android Service : bind or start?

Use startService() for services which will run independently after you start them. Music players are a good example. These run until they call stopSelf() or someone calls stopService().

You can communicate with a running service by sending Intents back and forth, but for the most part, you just start the service and let it run on its own.

Use bind() when the service and client will be communicating back and forth over a persistent connection. A good example is a navigation service which will be transmitting location updates back to the client. Binders are a lot harder to write than intents, but they're really the way to go for this usage case.

Regarding the priority: When all activities of a process lose their visibility, the process becomes a service process if it hosts a service which was started with onStart(), otherwise it becomes a background process. Service processes have a higher priority than background processes. Further details at the android developer site.

If a service process without visible activity needs a higher priority (e.g. a music player which really should not be interrupted), the service can call startForeground().



Related Topics



Leave a reply



Submit