START_STICKY for IntentService

Vinay Potluri picture Vinay Potluri · Jul 2, 2014 · Viewed 7.2k times · Source

I have seen many android Service examples where return START_STICKY is used to start an app on boot but is there anyway I can use the same for IntentService. I understand that Service method runs on the main UI thread and the IntentService as a separate thread.

  1. But how exactly can they be invoked and why is it not possible to start IntentService on boot. Since IntentService runs on a separate thread we have more control over it if i'm not worng.

  2. I tried using onStartCommand in IntentService but my app crashes on boot even though it works perfectly fine when started manually. Can we override onStartCommand in IntentService ?

Can someone help me with this ?

Answer

ianhanniballake picture ianhanniballake · Jul 2, 2014

Running at boot and START_STICKY have nothing to do with one another - START_STICKY is a flag to determine what should happen if your service is killed by Android.

IntentService is designed to process an incoming intent (via handleIntent) and stop immediately after. As seen in the source of IntentService, it already handles onStartCommand appropriately.

As long as you are requesting

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

and have the correct intent-filter on your IntentService:

<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>

Then your service will get called on boot complete.

(The one exception is if your app is installed on an SD card as per install location)