Android Question Tracking only with open app

nicieri

Active Member
Licensed User
Longtime User
I followed the following tutorial and it works fine, the question is, can everything be canceled automatically when the app is closed? What I want is for it to only work when the app is open.

The problem is that when the user closes the app the service continues to work, if I do it another way the app tries to start the service when it is already started and gets stuck.


Thanks
 
Solution
Ok, this is a little challenging because we are using a foreground service specifically to keep the tracking service running even if the app is pushed to the background so that android won't kill it. But we also want to detect if the user is trying to kill the app, so that we can then also stop the foreground service.

I just found this....place this code in the "Starter" service of your project:
B4X:
Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
    StopService(tracker)
End Sub

JohnC

Expert
Licensed User
Longtime User
1) Call the below line of code when the user exits the app:
B4X:
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):
B4X:
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
 
Last edited:
Upvote 0

nicieri

Active Member
Licensed User
Longtime User
I know, but if the user closes the app directly, where do you put this line? My code never executes it.

B4X:
StopService(Tracker)   'stops tracker service
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Put the StopService in the Activty_Pause event sub of your app.

And then restart the service in the Activity_Resume sub
 
Upvote 0

nicieri

Active Member
Licensed User
Longtime User
I need the service be canceled when the application closes, not when I send it to the background, in the background it must continue working.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
So when you say "when the application closes", how would the user close your application?

You would then put the StopService() call in the subroutine that gets called when the user closes the application.
 
Upvote 0

nicieri

Active Member
Licensed User
Longtime User
When the user presses the button to see all the apps that are open and close my app from there.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Ok, this is a little challenging because we are using a foreground service specifically to keep the tracking service running even if the app is pushed to the background so that android won't kill it. But we also want to detect if the user is trying to kill the app, so that we can then also stop the foreground service.

I just found this....place this code in the "Starter" service of your project:
B4X:
Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
    StopService(tracker)
End Sub
 
Last edited:
Upvote 0
Solution
Top