title: onDestroy() callback of Service
body: <p>In my Android project, I have a normal <code>Service</code>:</p>
  
  <pre><code>public class MyService extends Service{
    @Override
    public int onStartCommand(...){...}
  
    ...
  
    @Override
    public void onDestroy() {
      super.onDestroy();
      Log.d("MyApp","MyService onDestroy() is called!");
    }
  }
  </code></pre>
  
  <p>In my <code>BroadcastReceiver</code> class, I stop <code>MyService</code> &amp; do another task  :</p>
  
  <pre><code>public static class MyReceiver extends BroadcastReceiver {
     @Override
     public void onReceive(Context context, Intent intent) {
         context.stopService(new Intent(context, MyService.class));
         doAnotherTask();
     }
  }
  </code></pre>
  
  <p>According to my log, <code>onDestroy()</code> of <code>MyService</code> is executed after <code>doAnotherTask()</code> is done.</p>
  
  <p>How can I guarantee that <code>onDestory()</code> of <code>MyService</code> is executed before <code>doAnotherTask()</code> get called?</p>
  
  <p><strong>P.S.:</strong> I thought I could do something like:</p>
  
  <pre><code>boolean isStopped = context.stopService(new Intent(context, MyService.class));
  if(isStopped){
     doAnotherTask();
  }
  </code></pre>
  
  <p>But it could be possible that there is no service has been started, which means <code>stopService(...)</code> does nothing. So, I can't rely on my above code.</p>