1

I've an IntentService that should works indefinitely, even if application stops working.

This is my service:

public class TestService extends IntentService {

    public TestService() {
        super("test");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        while (true) {
            // do works
            SystemClock.sleep(60000);
        }
    }
}

And manifest:

<service android:name=".TestService" />

My service works well as long as I did not remove my application from the list of recent activities, but I want it works at all times.

Based on my searches I realized that if I set android:stopWithTask to false in the manifest and overriding onTaskRemoved method in the service, I'll be able to handle my service.

<service
    android:enabled="true"
    android:name=".TestService"
    android:exported="false"
    android:stopWithTask="false" />

But this requires API level 14 or higher and my app API level is 8.
How can I solve my problem?

Mousa Jafari
  • 677
  • 1
  • 6
  • 21
  • 2
    Off the top of my head, have your service run in a different process or prevent your app from showing up in recent app list. – Endor Dec 29 '14 at 07:22
  • I'm not sure. How can I run my service in a different process? – Mousa Jafari Dec 29 '14 at 07:26
  • Thanks @Endor. I realize that my service and app are in same process. – Mousa Jafari Dec 29 '14 at 07:32
  • look at the attribute of Service, android:process [here](http://developer.android.com/guide/topics/manifest/service-element.html#proc). But the answer below is right, you should consider using Service directly if you want it to run indefinitely. – Endor Dec 30 '14 at 02:35

1 Answers1

1

If you want service to be run indefinetely then why you have use IntentService. You should be using Service which will run indefinetlely. IntentService will be closing itself after completing the task it was asked to do and will pack another request from quene if any request comes.
Please check below link or link

Community
  • 1
  • 1
Roll no1
  • 1,315
  • 1
  • 16
  • 23