16

I am currently writing a android program which needs an IntentService. When I put the code in the onHandleIntent function, the code does not run, but it doesn't give errors in the MainActivity. But when I copy my code into the onStartCommand, it runs perfectly.

The problem is that I wanna know what are the differences between onHandleIntent and onStartCommand. Thanks.

CODE:

In onHandleIntent:

System.out.println("SERVICE STARTED! ! !");
//System.out.println(intent.getBooleanExtra("once", Boolean.FALSE));
if (intent.getBooleanExtra("once", Boolean.FALSE)) {
    Check();
}
mHandler.postDelayed(mRunnable, 3000);
Mauker
  • 11,237
  • 7
  • 58
  • 76
Chromium
  • 517
  • 1
  • 6
  • 21

3 Answers3

42

As from the docs:

The IntentService does the following:

  • Creates a default worker thread that executes all intents delivered to onStartCommand() separate from your application's main thread.
  • Creates a work queue that passes one intent at a time to your onHandleIntent() implementation, so you never have to worry about multi-threading.
  • Stops the service after all start requests have been handled, so you never have to call stopSelf().
  • Provides default implementation of onBind() that returns null.
  • Provides a default implementation of onStartCommand() that sends the intent to the work queue and then to your onHandleIntent() implementation.

And also:

All this adds up to the fact that all you need to do is implement onHandleIntent() to do the work provided by the client. (Though, you also need to provide a small constructor for the service.)

So an IntentService is a "Custom" Service with those special properties. So there's no need to override the onStartCommand(), actually, you shouldn't do it unless you're using the regular Service class.

Some example of IntentService usage:

Activity.java

Intent it = new Intent(getApplicationContext(), YourIntentService.class);
it.putExtra("Key", "Value");
startService(it);

YourIntentService.java

public YourIntentService() {
    super("YourIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {

    if (intent != null) {
        String str = intent.getStringExtra("key");
        // Do whatever you need to do here.
    }
    //...
}

You can also check this tutorial or this one for more info about Service and IntentService.

Also, check the docs.

Mauker
  • 11,237
  • 7
  • 58
  • 76
  • 3
    Wow, this was incredibly thorough. Nice answer! – AdamMc331 Jan 07 '16 at 03:44
  • I have gone through the tutorials, they are great! But is the problem that I haven't `@overide` the `onStartCommand` or the `onHandleIntent`? – Chromium Jan 07 '16 at 04:09
  • You told on the question that you did override the `onStartCommand`, and you shouldn't do that on `IntentService`. Use `onHandleIntent` instead, it's automatically called. – Mauker Jan 07 '16 at 04:11
  • no, I didn't override both of them. So the solution is that I put my code in the `onHandleIntent()` function, override it, and just leave `return Service.START_STICKY;` in the `onStartCommand()` function and not override it. Is that correct? – Chromium Jan 07 '16 at 06:50
  • 1
    Actually if you're using IntentService you shouldn't even bother to write a onStartCommand method at all. Put your logic inside the onHandleIntent instead. – Mauker Jan 07 '16 at 20:11
5

onStartCommand() is used when you use a Service. onHandleIntent() should be used instead when you use an IntentService. IntentService extends Service. And as per documentation

"You should not override this method(onStartCommand) for your IntentService. Instead, override onHandleIntent(Intent), which the system calls when the IntentService receives a start request."

If you have overridden onStartCommand(), that might be why your onHandleIntent() is not getting called.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Roshan
  • 3,442
  • 2
  • 14
  • 23
  • 3
    If you **do** override `onStartCommand()` when using `IntentService` you won't get your `onHandleIntent()` called later on, **unless** the last line of your `onStartCommand()` is `return super.OnStartCommand(intent, flags, startId)`. – GeertVc May 20 '17 at 14:14
2

You should not override onStartCommand() for your IntentService.

If you do, make sure to return super.onStartCommand(); because that sends the Intent to the work queue and then to your onHandleIntent() implementation.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
activedecay
  • 10,129
  • 5
  • 47
  • 71