Android Question Stopping a service that's rescheduled to restart

Discussion in 'Android Questions' started by rleiman, Dec 29, 2017.

  1. rleiman

    rleiman Well-Known Member Licensed User

    Hi Everyone,

    I'm calling a service from an activity. That activity has a button to start the service and another button to stop it. In the called service I'm using SetExactAndAllowWhileIdle to reschedule the service so it continues to run. Same thing happens when using StartServiceAtExact from within the same service.

    The user also wants to tap the stop button to stop that service and not allow it to reschedule itself until the start button is tapped. In my code I call StopService and it does stop the service but it still wakes up at the scheduled time.

    Can you tell me what coding I need to cancel the scheduled part of the service as well? Seems like there may be some sort of queue that needs to be emptied first through coding.

    In the mean time I think I will set some flag to stop the code in the service from executing based on the button tapped.

    Last edited: Dec 29, 2017
  2. Peter Simpson

    Peter Simpson Expert Licensed User

    Are you using the starter service?

    Dont forget to use Service.StopForeground if you are using Service.StartForeground.

    Are you sure that StopService iis being called when you are tapping on the button. You should put a break point at the StopService to make sure that it's being called.

    Also look at
    Sub Service_Destroy
    'Place your code here......
    End Sub
    Last edited: Dec 29, 2017
  3. rleiman

    rleiman Well-Known Member Licensed User

    Hi Peter,

    It's not the starter service.

    Thanks for the tip on using Service.StopForeground. Didn't know about that one. I will let you know how it works later.
  4. Erel

    Erel Administrator Staff Member Licensed User

    You can use CancelScheduledService keyword.
    rleiman and Peter Simpson like this.
  5. rleiman

    rleiman Well-Known Member Licensed User

    Hi Erel,

    Thanks. I just added it into the code for the button. I will let everyone know later how it goes.
  6. rleiman

    rleiman Well-Known Member Licensed User

    Hi Everyone,

    Thanks for all of your help. The app is now running quite smoothly now.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice