An IntentService is meant to run until it completes all the work you queue up for it (via start commands/intents). It has no mechanism to stop this background work when you stop the service prematurely. If you do not want this behavior, then you should not use IntentService as-is. Instead you can implement your own Service-with-a-worker-thread, for instance by subclassing IntentService itself.
In your own implementation you can add functionality in to stop the background work as soon as the service is destroyed. Actually, it is really important you do this, because once you call stopService() from your activity (which, unlike stated elsewhere will perfectly work with any Intent that resolves to your service and does not need to be the intent you started it with), your service gets destroyed and nothing is preventing the Android framework from killing your process (that is still running your worker threads). So it is best you nicely join your worker threads or at least let them stop somehow.
An example of an IntentService extension that will stop background processing:
public class MyIntentService extends IntentService {
private static final String TAG = "MyIntentService";
private volatile boolean destroyed;
public MyIntentService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
while (!destroyed) {
Log.d(TAG, String.format("still running, tid=%d", Thread.currentThread().getId()));
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
} catch (InterruptedException e) {
Log.e(TAG, "interrupted while sleeping");
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
super.onDestroy();
destroyed = true;
}} //sorry i don't know how to put this last bracket on next line in SO ;-)
When you include it in your project and start/stop it with any intent you will see that the print "still running" will stop once you call stopService();