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!
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, usebindService()in theonStart()methods of the activities that need the service, and callunbindService()in their correspondingonStop()methods. You can useBIND_AUTO_CREATEto have the service be lazy-started when needed, and Android will automatically stop the service after all connections have been unbound.