Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, this isn't a very good answer, but why wouldn't there be something else between your <code>bindService()</code> and <code>onBind()</code> calls? You seem to be assuming that when you call bind the system will immediately create and start your service, and no such guarantee is provided.</p> <p>You haven't indicated whether yours is an <code>IntentService</code>, or just a regular Service, and this might affect the speed with which your service is launched, but my answer would be the same.</p> <p>Also, I'm assuming that your <code>ServiceConnection</code> object is called, as expected, sometime after your service's <code>onBind()</code> returns.</p> <p>I say this not based on having read anything definitive, but because I had a similar experience: I was surprised and annoyed at how long it took before my service was created and my <code>ServiceConnection</code> was called, even though I returned from the service's onBind very quickly. (My fragments needed data from the service to create the initial UI so any delay in the creation of the service meant a delay in displaying the initial UI to the user.)</p> <p>In the end I settled upon launching my service using an Intent rather then a bind, e.g.</p> <pre><code>Intent si = new Intent( this, com.company.MyService.class ); si.setAction( MyService.SVC_BIND ); startService( si ); </code></pre> <p>I then sent <code>MyService.SVC_UNBIND</code> in place of calling unbind. When I received the UNBIND intent in my service I cleanup and then call <code>stopSelf()</code>. These are user actions - I just named them for how I'm using them.</p> <p>I believe this was faster, but looking back upon my comments from that code I don't have any specifics. Note that this meant no <code>ServiceConnection</code>, but I'm making some direct calls from the activities into the service, and using <code>LocalBroadcastManager</code> a fair bit.</p> <p>Another option to consider (in order that your service be started more quickly, if that is your goal here??) is to launch it in <code>Appliction.onCreate()</code>, rather then waiting for <code>Activity.onResume()</code>. All of these options make it necessary to do some extra work to determine when to stop the service, compared to your current, normal, scheme where that is taken care of for you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload