1) Call the below line of code when the user exits the app:
StopService(Tracker) 'stops tracker service
2) As a backup to make sure the GPS service doesn't run in the background, create a persistent app preference setting called "RunInBackground" that will serve as a flag to determine if the background GPS service should run or not.
When the user starts the app, set this flag to "True" and save this preference value.
When the user exits the app, set the flag to "False" and save this preference value.
Then when the GPS service tries to start, it will check the value of this flag to see if it should run or not (because the user closed the app, so the flag = false):
Sub Service_Start (StartingIntent As Intent)
Dim RunInBackground as boolean
'set value of RunInBackground to a stored preference value
RunInBackground = [retrieve value from app preferences]
If RunInBackground = False then
StopService("") 'stops current service
Else
'run service code normally
End if
End Sub