I have a service in my Android Application which runs always.Now i have a settings from my server through GCM and update these settings to my service. I put my settings in oncreate of service. So i need to restart my service to fetch latest settings. How to restart my service?
If you call startService() multiple times, the service will be called with onStartCommand() multiple times, one per call to startService() . Whether it will "restart" will depend upon whether the service was still considered to be running from the previous startService() call.
Absolutely Correct. Only one instance of Service is created for an application process. And when you call StartService(); again, then only onStartCommand() gets called and new Intent is passed to onStartCommand() method. Note: onCreate() is not called again.
A service is started when an application component, such as an activity, starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. A service is bound when an application component binds to it by calling bindService().
Services in Android are a special component that facilitates an application to run in the background in order to perform long-running operation tasks. The prime aim of a service is to ensure that the application remains active in the background so that the user can operate multiple applications at the same time.
Call this two methods right after each other, which will causes the Service
to stop and start. Don't know any method that "restarts" it. This is how I have implemented it in my application.
stopService(new Intent(this, YourService.class)); startService(new Intent(this, YourService.class));
The best solution is to use onDestroy().
When need to restart the service just call
RESTARTSERVICE = true; stopService(new Intent(this, CLASS_OF_SERVICE.class));
and
public void onDestroy() { if (RESTARTSERVICE) { startService(new Intent(this, CLASS_OF_SERVICE.class)); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With