Android Question Stopping a persistent foreground service

Brian Dean

Active Member
Licensed User
Longtime User
I used the example given here to build a media-player service, although I did not really understand why it worked. But it works very well - so well that I cannot find a way to stop it when I want to.

I have also read this post which might or might not be relevant, and again I don't really understand it. I have tried using Service.StopForeground and StopService(Me) but that has the opposite effect; I get multiple instances like some mythological monster that grows two heads for every one you cut off.

So, having started a persistent foreground service, how do I stop it?
 

ac9ts

Active Member
Licensed User
Longtime User
Have you tried using StopService from the same activity that you started the service from?
 
Upvote 0

Brian Dean

Active Member
Licensed User
Longtime User
@ac9ts - Well, I have just tried what you suggest. Service Destroy runs, but other than that it doesn't even blink - just goes on playing.
 
Upvote 0

Brian Dean

Active Member
Licensed User
Longtime User
@ac9ts : What you said might have given me a clue. I am not sure where the Service is getting started; that is the problem when you don't really understand what you are doing. I will take a another look and maybe re-post with some sample code if I cannot make sense of it. Thanks for your suggestion.
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
Try stopping and releasing the player in the Service_Destroy sub

Upload the project (File -> Export as Zip) for us to take a look at what's what.
 
Upvote 0

Brian Dean

Active Member
Licensed User
Longtime User
I attach a modified form of my project. It runs from the Assets file so it will run on any device - it doesn't have to scan for mp3 files. I have disabled the id3 tag reader so the the app looks a bit bare but it still illustrates my problem.

Question : How should I stop the player from within the app? I have left in one of my attempts :

B4X:
Public Sub stop
    Service.StopForeground(id)
    StopService(Me)
End Sub

Rather than stop the Player service this seems to create yet another instance, which is quite bizarre. This app is just for my own education, so not important, but any guidance would be appreciated.
 

Attachments

  • MP3Player.zip
    36 KB · Views: 86
Upvote 0
Top