title: IntentService onHandleIntent() still running after onDestroy() fired
body: <p>In my preference screen, I want to start a service to download files from the internet when one of the preference is being clicked. If the service is already running (downloading files), then the service should be stopped (cancel download).</p>
  
  <pre><code>public class Setting extends PreferenceActivity {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      downloadPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  
          @Override
          public boolean onPreferenceClick(Preference pref) {
              if (DownloadService.isRunning) {
                  Setting.this.stopService(new Intent(Setting.this,
                      DownloadService.class));
              } else {
                  Setting.this.startService(new Intent(Setting.this,
                      DownloadService.class));
              }
              return false;
          }
      });
      }
  }
  </code></pre>
  
  <p>The service class:</p>
  
  <pre><code>public class DownloadService extends IntentService {
  
  public static final int DOWNLOAD_SUCCESS = 0;
  public static final int DOWNLOAD_FAIL = 1;
  public static final int DOWNLOAD_CANCELLED = 2;
  public static final int SERVER_FAIL = 3;
  
  public static boolean isRunning = false;
  private int result;
  
  public DownloadService() {
      super("DownloadService");
  }
  
  @Override
  public void onCreate() {
      super.onCreate();
      isRunning = true;
  }
  
  @Override
  protected void onHandleIntent(Intent intent) {
      if (NetworkStateUtils.isInternetConnected(getApplicationContext())) 
          result = downloadFiles(getApplicationContext());
  
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      switch (result) {
      case DOWNLOAD_SUCCESS:
          Toast.makeText(getApplicationContext(), R.string.download_finished,
                  Toast.LENGTH_SHORT).show();
          break;
      case DOWNLOAD_CANCELLED:
          Toast.makeText(getApplicationContext(), R.string.download_canceled,
                  Toast.LENGTH_SHORT).show();
          break;
      case DOWNLOAD_FAIL:
          Toast.makeText(getApplicationContext(), R.string.download_failed,
                  Toast.LENGTH_SHORT).show();
          break;
      }
      isRunning = false;
  }
  }
  </code></pre>
  
  <p>This service is meant to run until download has finished. The function <code>downloadFiles()</code> uses no <code>AsyncTask</code>. It saves the <code>HttpURLConnection</code> with an <code>FileOutputStream</code> directly.</p>
  
  <p>The service started correctly when I click the preference. Now the problem is, when I click to stop the service with <code>stopService()</code>, <code>DownloadService</code> triggered <code>onDestroy()</code> immediately; however according to logs, <code>onHandleIntent()</code> is still running becasue I can still see HTTP requests continuously. Is this because <code>Service</code> runs in a thread itself, or am I doing something wrong? How can I ensure that everything in <code>onHandleIntent()</code> stops immediately (or at least able to stop) when <code>stopService()</code> is being called?</p>