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

John Decowski

Member
Licensed User
Longtime 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.
 

Peter Simpson

Expert
Licensed User
Longtime User
@John Decowski Have you tried something like this???

B4X:
Sub Activity_Pause (UserClosed As Boolean)
     CancelScheduledService(<Your Service Name>)
End Sub
 
Upvote 0

John Decowski

Member
Licensed User
Longtime 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.
 
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0

John Decowski

Member
Licensed User
Longtime 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.
 
Upvote 0

Brandsum

Well-Known Member
Licensed User
You can track from starter service.
B4X:
Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
    StopService(<your service>)
End Sub
 
Upvote 0

John Decowski

Member
Licensed User
Longtime User
You can track from starter service.
B4X:
Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
    StopService(<your service>)
End Sub
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!
 
Upvote 0
Top