Android Question [SOLVED] how to detect if main app has been closed .

Discussion in 'Android Questions' started by John Decowski, Aug 17, 2019.

  1. John Decowski

    John Decowski Member Licensed User

    I have an app with a background service. I am using the service to play sound in background. Problem is, is when you close the app manually, the service keeps playing sound in background. I have the service set to start every 10s and audibly play sound every 10s. The service schedules itself every 10s so , when the main activity is closed the service still goes on rescheduling. and yes i have watched the app life cycle tutorial.
     
  2. Peter Simpson

    Peter Simpson Expert Licensed User

    @John Decowski Have you tried something like this???

    Code:
    Sub Activity_Pause (UserClosed As Boolean)
         
    CancelScheduledService(<Your Service Name>)
    End Sub
     
    John Decowski likes this.
  3. John Decowski

    John Decowski Member Licensed User

    @Peter Simpson that does work to kill the service but, I need to kill the service when the main activity is killed unexpectedly.
    because i need the service to still run if the home button is pressed.
     
  4. mcqueccu

    mcqueccu Active Member Licensed User

    Usually pressing back key twice should close the main activity and the app.

    I have a radio streaming app which plays in the background with whena service you press the home button, but it has a notification icon which you can press to bring the main activity up and then close with back button.
     
  5. John Decowski

    John Decowski Member Licensed User

    my concern is when the user opens the task chooser and quits app, my service stays running using data and other resources. I want to be able to catch the quit and close out the service.
     
  6. Brandsum

    Brandsum Active Member Licensed User

    You can track from starter service.
    Code:
    Sub Service_TaskRemoved
        
    'This event will be raised when the user removes the app from the recent apps list.
        StopService(<your service>)
    End Sub
     
    John Decowski likes this.
  7. John Decowski

    John Decowski Member Licensed User

    Thank you @Brandsum that is exactly what i was looking for! I've really never messed with services much and starter service either. Thank you!
     
Loading...
  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