1

my question is simply about how do we restart an android service that runs in background in it's own thread from an activity if the service stops itself after completing certain tasks.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Umar Iqbal
  • 669
  • 1
  • 11
  • 31
  • Use the alarm manager [ how to schedule some code execution in android or: what exactly are daemon threads in android?][1] [1]: http://stackoverflow.com/questions/3883246/how-to-schedule-some-code-execution-in-android-or-what-exactly-are-daemon-threa – Amresh Jan 31 '14 at 07:55
  • Actually i want it running after a certain call from activity(Only if it has stopped) and to remain active until it completes it work even if the activity is long dead. – Umar Iqbal Jan 31 '14 at 10:19

2 Answers2

0

Simply call startService() from your activity:

http://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)

Philio
  • 3,675
  • 1
  • 23
  • 33
  • But it won't be a background service then? would it be? – Umar Iqbal Jan 31 '14 at 07:54
  • A service can't be foreground, they always run in the background. It doesn't matter that you start it from an activity. – npace Jan 31 '14 at 08:06
  • Actually that's not strictly true, but it's a special case: http://developer.android.com/guide/components/services.html#Foreground – Philio Jan 31 '14 at 08:21
  • @Philio Simply I'm trying to ask if I start it from my activity using your method then will my service be active after the activity is long dead? – Umar Iqbal Jan 31 '14 at 10:21
  • It depends on the service, you need to override onStartCommand to achieve that. – Philio Jan 31 '14 at 11:33
-1

Make sure you are extending Service, not IntentService. And then start service in STICKY mode. It should remain active that way. Even if it dies for some reason (system may kill it etc.), it should restart on its own.

Read this link: START_STICKY and START_NOT_STICKY

Community
  • 1
  • 1
pree
  • 2,297
  • 6
  • 37
  • 55