START_STICKY for IntentService

I have seen many examples of android applications where START_STICKY is used to launch the application at boot, but in any case I can use it for IntentService. I understand that the service method runs in the main thread of the user interface, and IntentService in a separate thread.

  • But how exactly can they be called and why is it impossible to start IntentService at boot. Since the IntentService runs in a separate thread, we have more control over it, if I'm not worng.

  • I tried using onStartCommand in IntentService, but my application crashes on loading, even if it works fine when it starts manually. Can we override onStartCommand in IntentService?

Can someone help me?

+4
source share
1 answer

Run at boot time and START_STICKYhave nothing to do with each other - START_STICKYthis is a flag that determines what should happen if your service was killed by Android.

IntentServicedesigned to handle incoming intent (through handleIntent) and stops immediately. As shown in the IntentService source , it already processes accordingly onStartCommand.

While you request

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

and you have the right intent filter on 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 be called at boot.

( - SD- )

+8

Source: https://habr.com/ru/post/1546897/


All Articles