Skip to content Skip to sidebar Skip to footer

How To Stop Background Service Automatically When App Is Not In Use?

Now i am going on with my notification part for that using BackGround Service in current the service will run even after my app get stopped or either i use other app But Now what i

Solution 1:

I would go with another approach:

  • Use onPause and onResume methods to track state of your Activities.
  • When your last Activity is being paused, start a timer task with 5min delay.
  • In the task stop the service.
  • If you hit onResume method and the task is not null, clear the task.

Solution 2:

It's still not clear exactly what you are trying to do and why. If your service is a subclass of Service then it can stop itself or your activities can explicitly stop it. If your activities bind with it rather than calling startService(), the system can automatically stop the service once all clients have been unbound. Android is built so that it can start and stop them as needed. Note that even if your service and all activities are stopped, your app's process will stick around until the system needs resources.

Post a Comment for "How To Stop Background Service Automatically When App Is Not In Use?"