tags:

views:

86

answers:

2

Is it possible to stop all started services when the user hits the Home Button?

I use:

startService(new Intent(ClassName.this, ClassName2.class));
stopService(new Intent(ClassName.this, ClassName2.class));

This means I will have to somehow add the 'stopService()' for 7+ of my app classes I've researched this topic and I think there's 'onTerminate' but still not sure how this should be implemented.

Any help or hints would be appreciated! Thanks!

+3  A: 

Is it possible to stop all started services when the user hits the Home Button?

Not directly. For starters, you have no way of knowing they pressed HOME.

If you only want the service running while activities are using it, consider getting rid of startService(). Instead, use bindService() in the onStart() methods of the activities that need the service, and call unbindService() in their corresponding onStop() methods. You can use BIND_AUTO_CREATE to have the service be lazy-started when needed, and Android will automatically stop the service after all connections have been unbound.

CommonsWare
Thanks for the response.This all makes sense, one thing though, is there more information on "BIND_AUTO_CREATE" ? I understand the 'service' concept, but still not sure how would I keep the music playing between my 7+ Activities, but stop it when user gets out to the 'Home Screen'. If anyone else has the same problem, I found a good article about Android services here: http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-iii-android-services/
Rad The Mad
Your desired pattern will be difficult to implement. That's why most music players don't do what you do -- they specifically *want* the music playing when the user presses HOME, offering a Notification for rapid access back to the app to stop the music. In fact, it will be impossible for you to implement this pattern specifically for HOME. You can probably implement this pattern for "none of my activities are in the foreground", by keeping a flag of whether any of your activities are in the foreground, plus a watchdog that shuts the music if the flag is false for too long.
CommonsWare
Yeah, this is just for a game, so when the game is suspended/paused the music will not play.I noticed that I can use onPause() for it to stop playing if I press 'HOME' but the only problem is that it stops when I proceed in the game menu.Any hints to solve this issue?
Rad The Mad
I might just drop the whole services idea and just do soemthing like this: http://karanar.net/?p=25
Rad The Mad
+1  A: 

If you want services to stop when the user leaves your app, I would ask if you want to use services at all. You may just be making your application way more complicated than it needs to be.

Also, this line is really questionable:

startService(new Intent(ClassName.this, ClassName2.class));

You are making an Intent whose action is the class name of one class, and data URI is the class name of another class...! Maybe you mean something like "new Intent(context, MyService.class)"?

hackbod
Thank you for your input, I'm just trying to have music playing in my app. I also have music/sound settings to turn it off and etc. I will also fix the "new Intent" part.
Rad The Mad
Okay. You don't need a service to have music playing, though.
hackbod