Development issue/problem:

I would like to know if it is possible to have a service that starts with startService and then connect to this service and make a number of remote procedure calls?

these two services have different life cycles, so it’s not possible, anyone know?

How can I solve this problem?

Solution 1:

I think Hara’s answer was a little confusing. What you describe is perfectly legitimate and is indeed the only way to achieve the desired behavior. When you create a service per connection, it dies when you disconnect it. Therefore, the only way to keep it unbound is to start StartService(). There is no conflict with life cycles because it is only about how the service started. So, once it starts StartService(), it follows this life cycle. So you can tie and untie as much as you want, and it will only die if you call stopService() or stopSelf().

Solution 2:

Yes, you can run and pair the same service (one or more times).

The following flowchart shows how the service life cycle is managed. The variable counter makes it possible to follow the number of connected customers:
Fill in the image description here

A good example is the music application. Explanation of the official tutorial for creating a media browser :

A service that is not linked (and does not work) is destroyed when all
‘s of its customers are unrelated. If your UI activity is currently disabled, the
service will be destroyed. It’s no problem if you’ve never played
music before. However, when playback starts, the user probably expects to be able to continue listening even after switching to another application. You do not want to destroy the drive when you disconnect the user interface to work with another application.
Therefore you need to make sure that the service starts when
starts reading by calling startService(). A continuous service must be explicitly terminated by
, whether or not it is linked. This way your
player will continue to work, even if the link to the
control interface is removed.
To stop a running service, call Context.stopService() or stopSelf().
The system will be shut down and destroyed as soon as possible.
However, if one or more customers are still connected to the service, the
call to end the service will be delayed until all customers are disconnected.

From the arbitration service:

The service can be launched or have a
connection. The
system will then maintain the
service as long as the
system is running or one or more
connections to the
Context.BIND_AUTO_CREATE flag are present. None of these situations applies after
, the onDestroy() method of service
is referred to as
and the service effectively terminates

Solution 3:

If you start the service with startService(), you must stop it with stopService().

There are two reasons why the service can be performed by the system. When someone calls Context.startService(), the system retrieves the service (creates it and calls the onCreate() method if necessary) and then calls the onStartCommand(Intention, intention) method with the arguments provided by the customer. At this point, the service continues to run until Context.stopService() or stopSelf() is called. Note that multiple calls to Context.startService() are not nested (although they result in multiple corresponding calls to onStartCommand()), so no matter how many times the service is started, it will stop after the call to Context.stopService() or stopSelf(); however, services may use their stopSelf(int) method to ensure that the service does not stop before the initiated intention is processed.

With bindService() you can bind as many ServiceConnections to the service as you want, but be careful with the passed flag. If you pass 0, when stopService() is called, the service is stopped (I don’t know exactly what happens to your ServiceConnection). Otherwise, if you want your service to live until a ServiceConnection is connected to it, use BIND_AUTO_CREATE.

comes from the stopService() function:

Ask if you want to stop this application service. If the service doesn’t work, nothing happens. If not, he resigns. Note that calls to startService() do not count – it stops the service no matter how often it is started.
Note that a discontinued service to which ServiceConnection objects are still linked with the parameter BIND_AUTO_CREATE will not be destroyed until all these links have been deleted. For more information on the service life cycle, see the service documentation.
This function starts a SecurityException if you do not have permission to stop this service.

I hope this helps you…

Good luck!

stop bound service android,android bindservice,service onbind,android unbindservice,stop service android,difference between startservice and bound service,android messenger example,serviceconnection android,when to use bound service android,startservice android,bind foreground service android,service lifecycle android,background service in android,android-service communication,android background service always running,android foreground service example,android background service notification example