On click of a button I want to start service using method startService(new Intent(currentActivity.this,MyService.class)) but if service is running I don’t want to call this method to avoid run service that is already running.How this is possible.I am using both Intent service and Service in same project and want to apply same condition for both.
On click of a button I want to start service using method startService(new Intent(currentActivity.this,MyService.class))
Share
A service will only run once, so you can call
startService(Intent)multiple times.You will receive an
onStartCommand()in the service. So keep that in mind.Source:
Note that multiple calls to
Context.startService()do not nest (though they do result in multiple corresponding calls toonStartCommand()), so no matter how many times it is started a service will be stopped onceContext.stopService()orstopSelf()is called; however, services can use theirstopSelf(int)method to ensure the service is not stopped until started intents have been processed.At: http://developer.android.com/reference/android/app/Service.html on topic: Service Lifecycle