Android Question How to Stop Service when quit from App

incendio

Well-Known Member
Licensed User
Longtime User
Hi guys,

I run a service by put StartServiceAt command under Sub Service_Start.

How do I stop this service when quit from app?

I put this code when quit from app
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'return true if you want to consume the event
    If KeyCode = KeyCodes.KEYCODE_BACK Then
         StopService(MyService)
         ExitApplication
    End If
End Sub

But the service still running, the only way to stop it is reboot my device.
 

udg

Expert
Licensed User
Longtime User
Hi,

for a scheduled service StopService will not work since its is intended to stop a currently running service.
Try the following:
B4X:
StopService(yourservicehere)
CancelScheduledService(yourservicehere)

udg
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…