0

My Android application exists of a main activity in which the user has the ability to start several other activities. In order to have a permanently working keyword recognition (speech recognition), I use an IntentServicethat is started in the onCreate() of the main activity. The service needs to be working as long as any activity of the app is in the foreground.

However, this service uses the microphone all the time, so it's desirable to stop the service once the user returns to his/her home screen. I know the basics about activity lifecycles, but my question is: what is the best way to stop the IntentService when the user returns to the home screen from any activity?

To restart the service when the app is re-opened, I was thinking of a superclass that all activities inherit from. In the superclass I would set a static boolean serviceStopped that is set to true when the user goes to his/her home screen (and the service is stopped). In the onResume() method, this boolean will be checked and the service will be restarted if needed.

J.A.
  • 1
  • 1
  • Possible duplicate of [How to force an IntentService to stop immediately with a cancel button from an Activity?](https://stackoverflow.com/questions/11258083/how-to-force-an-intentservice-to-stop-immediately-with-a-cancel-button-from-an-a) – Mark Mar 20 '18 at 15:37
  • That question is more about how to stop an IntentService in general. My question is more like: how to stop (doesn't matter how) everything from the app, including the IntentService, when a user goes his to home screen. – J.A. Mar 20 '18 at 16:03

1 Answers1

0

As I know there is no way to detect if the user leave your application and is in the home screen.

I think there is no a simple way to detect if your application is open/closed; however I found this How to get the list of running applications? and I think it could help you.

You could also try to start a Service in the background and check when any of your activities start or is destroyed.

Raul Cabrera A.
  • 169
  • 2
  • 6