I am developing an app where I start a service if a particular feature (in my app) is enabled by user. I want to keep running this service even after user quits my app. How can I do this? plz help
-
1Don't turn off the service when the user exits the app? Running in the background without a foreground activity is kind of what Services are meant for. Can you clarify what your problem is exactly? – DeeV Feb 24 '12 at 16:24
3 Answers
Right from developer.android.com
A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it. A service is implemented as a subclass of Service and you can learn more about it in the Services developer guide.
So, as long as you create a service, and the user exits your app it will still run. Just like the example above.
This should give you all the information you need: https://developer.android.com/guide/components/services.html
-
9This will not keep the service going on if a Task Manager, which are quite common on phones now, kills the service. The onStartCommand(...) in the service needs to return START_STICKY – user1406716 Mar 13 '14 at 10:15
-
2
-
-
i created a notification builder in my service and it only works when my application is open. I'm wondering if I should call the `onCreate()` in my service? – Kairi San Feb 19 '16 at 03:39
-
The simplest way worked for me while using music player was start the service as startForeground(notificationId, notification); When u wanna stop it just simply call stopForeground(true); – Ankush Joshi Jan 07 '17 at 14:32
you can use Alarm manager for any background service.
For particular timing you want to perform something which can be service.
And for the continues process,You can start service from alarm manager.

- 22,255
- 15
- 63
- 104
One of the android suggested way :
A started service can use the startForeground(int, Notification). API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)
Other way is restart the service when its stopped (System will call onDestroy() on service which is stopped)

- 1,431
- 16
- 22