Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep a service running in background even after user quits the app? [closed]

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

like image 897
Saurabh Agrawal Avatar asked Feb 24 '12 16:02

Saurabh Agrawal


People also ask

How can I make a service run continuously on Android?

1: You have to invoke the service's startForeground() method within 5 seconds after starting the service. To do this, you can call startForeground() in onCreate() method of service. public class AppService extends Service { .... @Override public void onCreate() { startForeground(9999, Notification()) } .... }

How do I stop an android application from closing?

In android, by pressing a back button or home button. So put an event key listener for back & home button and terminate the service.


2 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

like image 65
EGHDK Avatar answered Oct 10 '22 17:10

EGHDK


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.

like image 29
Bhavesh Hirpara Avatar answered Oct 10 '22 18:10

Bhavesh Hirpara