Search code examples
androidservicealarmmanager

stop service when my app is not running in background


I want to check new messages every 10 mins in the background using alarmManager/Service(I have written the code for the same) but since service runs even if the app is alive or dead .I want the service to stop if app is not running in the background .How do I do this ?

Please Help. Thanks in Advance.


Solution

  • Stop your service on the onPause state of your activity.

    Visit http://developer.android.com/reference/android/app/Service.html for more details about services.

    For AlarmManager visit http://developer.android.com/reference/android/app/AlarmManager.html for more deailts. There you can find cancel method to remove any alarm that matches your intent.