Android: How to get onStartCommand () to be called before onBind ()?

I am trying to create a sticky service that is tied to (I need to run potentially asynchronous operations in the background on some data that is stored in the service). To do this, I need to be sure that onBind always works after onStartCommand. Is there any way to guarantee this?

+6
source share
4 answers

Of your requirements, you probably don't need to get attached to your Service . Then it’s enough to use IntentService , since this service will stop after shutdown.

Adapted from the documents:

IntentService is the base class for services that process asynchronous requests (expressed as intentions) on demand. Clients send requests through startService (Intent) calls; the service starts as needed, processes each intention, in turn, using the workflow and stops when it ends.

IntentService example:

 public class MyService extends IntentService { @Override protected void onHandleIntent(Intent intent) { if (intent != null) { // Do some work here, get Intent extras if any, etc. // ... // Once this method ends, the IntentService will stop itself. } } } 

More information on how to create an IntentService can be found here .

This can handle your asynchronous operations. And if you need any feedback that will β€œbreak” the asynchronous part of the requirement, you can use the LocalBroadcastManager or, as you said, you can bind the Service to this. And again, it depends on what you are trying to do.

From the documents you have two types of services.

Start

A service starts when an application component (for example, activity) starts it by calling startService (). After starting, the service can run in the background for an indefinite period, even if the component that began to be destroyed. Typically, a running service performs a single operation and does not return a result to the caller. For example, it can upload or download a file over the network. When the operation is completed, the service should stop.

Connected

A service is bound when an application component calls bindService (). A related service offers a client-server interface that allows components to interact with the service, send requests, receive results, and even do it through processes with interprocess communication (IPC). The linked service only works as long as another application component is bound to it. Several components can immediately communicate with the service, but when they all turn off, the service is destroyed.

Reminder:. You can start the Service through startService() so that it runs "indefinitely" and communicates with it by calling onBind() later.

 Intent it = new Intent(this, MyService.class); startService(it); // Start the service. bindService(it, this, 0); // Bind to it. 

If you want to just start this service while your Activity running, you can just call onBind() .

 Intent it = new Intent(this, MyService.class); bindService(it, this, 0); // This will create the service and bind to it. 

More information about the "default" Service , about how to use and implement it, can be found here .

Just choose what works best for your use case and you're good to go.

+7
source

The fact is that you should not call both startService () and bindService (). If you want to bind to a service, call the bindService () function. When the service is connected, the implementation of ServiceConnection.onServiceConnected () is called, providing you with an IBinder service. Typically, a related service is used as the server side of the client-server internal interface. You connect to the service, return an IBinder that gives you a handle to the Service object itself, and then use the Service handle to pass data or call methods to the service.

Linked services are almost always used for inter-process communication (IPC).

+1
source

According to docs :

A service can essentially take two forms:

Start
A service starts when an application component (for example, activity) starts it by calling startService ().
Connected
A service is bound when an application component communicates with it by calling bindService (). [..] The linked service only works as long as another application component is bound to it. Several components can immediately communicate with the service, but when they all turn off, the service is destroyed.

To ensure that onStartCommand() always runs before onBind() , continue to send the new intent to the service anytime you want to bind it. This is because any new intent for the service will call onStartCommand() , then a call to bindService() will execute onBind() .

0
source

For any solution to the conservation problem. You can call the start function in the onBind method.

 @Override public IBinder onBind(Intent intent) { //stars self Intent myIntent = new Intent(this, Service.class); startService(myIntent); //TODO return null; } 

Then you just check if the service is running or not, and process it.

0
source

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


All Articles