I have an application which is doing some repetitive background tasks. I'm using service and Alarmmanager with setrepeating method. The only way to stop this service is a button which appears on my activity screen (of course android system can kill it for any reason at any time but I don't care what android system does). My problem is when my activity is destroyed by android system after a while, I have a service which is running forever unless Android kills it. My question is how can I get a reference of this service to stop it after creating new activity(I assume I'm creating new activity after the old one destroyed)?.
-
http://stackoverflow.com/questions/27459885/stop-service-in-an-activity this may helpful – Mousa Jafari Dec 14 '14 at 22:12
-
I think I've just created duplicate of this question http://stackoverflow.com/questions/10482171/how-to-get-reference-to-running-service – mbaydar Dec 14 '14 at 23:00
3 Answers
Consider bindService and unbindService.
When your activity is created, you can call bindService
, and use the returned binder in order to communicate with your service. A service can be both bound and started, in case you don't want unbind to kill your service after your activity ends.
Another approach could be using a LocalBroadcastManager in order to communicate between your activity and service.
Finally, consider either Otto or Eventbus - two awesome opensource projects for communicating between components in your android project.

- 525
- 6
- 6
-
I'm not sure if these work for my problem. I can make communication between activity and service. Problem is after my activity is destroyed I can't stop my service. I also don't want to user binding because I can't control when is gonna destroyed my activity and even after my activity destroyed I want my service keep working unless user stop it. – mbaydar Dec 14 '14 at 22:37
-
I think you need to be more clear on what it is you do want. Your question seems to say you want to kill the service when the activity is destroyed. If so, binding is a good answer. If you want something else, tell us what it is you do want. – Gabe Sechan Dec 14 '14 at 22:59
-
if I understand correctly, @mbaydar wants to be able to reconnect to his background service once his activity is destroyed and then recreated. There are two options here. 1. Starting the service AND binding to it, thus ensuring that unbind won't kill the service, but if the service is already running, he'll simply bind to the already running instance. 2. Sending a broadcast event(/otto/eventbus) to "any service that might be out there", and waiting for a reply. – Daniel Kopitchinski Dec 14 '14 at 23:03
Use the bind service and unbind service methods on your MainActivity so that you can call unbind service onDestroy()
when ever your apps activity is either explicitly destroyed by user or the android system, the service class is destroyed alongside.
Use this post as a guide: Example: Communication between Activity and Service using Messaging
-
If I use bind service it means as soon as my activity is destroyed my service is also going to destroyed. Which is I don't want because I can't control when my activity is destroyed. In other words I can't make my activity lives as long as I want at the background. So I don't think the answer is binding. – mbaydar Dec 14 '14 at 22:31
-
The reason why your service class is running forever without closing even when a new activity is created is simply because the service class is not sync(no form of communication) with any activity in the first place when you launch the app for the first time. – CodeZero Dec 14 '14 at 23:00
I think I couldn't explain my problem clearly. But I found my answer at this post How to get reference to running service? Dawid Sajdak answer
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("your.service.classname".equals(service.service.getClassName())) {
return false;
}
}
return true;
}
if(isMyServiceRunning()) {
stopService(new Intent(ServiceTest.this,MailService.class));
}