StopService Not Stopping Service

johnv727

Member
Licensed User
Longtime User
I currently have a service module that looks into a sqlite database for a list of queued files to upload to a cloud server. Previously I had it setup to upload files to an FTP server and up until these past few days, I can't get the service to stop after calling the "ServiceStop("")" sub like I did in the past. I did try out running the service in the foreground and once I did that, I couldn't get the foregound service to stop with a Service.Stopforeground(1) code. Then I changed it back to how it was before and now no matter what I do the service won't end. So once it'd done looping through and uploading all the necessary files and they are deleted from the phone, the call to get data from the table in the sqlite database throws an exception since there is no data to pull and the server crashes. It's like the "StopService" call is getting skipped over or ignored. It honestly doesn't make sense why it won't work now when the same code worked before.

What could cause the service not to stop even when it is told to? I even tried deleting the module, creating another one, and pasting in the previous text.
 

johnv727

Member
Licensed User
Longtime User
That's the thing, the Service will not destroy. Even if i put "ServiceStop("")" as the first line in the Service_Start sub. Honestly doesn't make too much sense to me as to why it started doing that. What ends up killing the sub is when an exception gets thrown from later code from an array going out of bounds due to the service not stopping.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
EDIT: Crap - I've done some more research on this & found this:

"Also, bear in mind that the exact timing of the service being destroyed is up to Android and may not be immediate. So, for example, if you are relying upon onDestroy() to cause your service to stop some work that is being done, consider using another trigger for that"

So my post below might be somewhat redundant...

I'm having the same problem. I have a service (NetConnect) that sets up a peer network between 2 devices running my Yahtzee game (yes I know I can use WiFi Direct for this, but I don't). When a user attempts to start a network game, I call StopService(NetConnect) to ensure that any previous connection is killed before I try to establish a new one, however I've just realized that Service_Destroy is not executing. Here's the code in my Main module:

B4X:
Sub btnStart_Click
    StopService(NetConnect)
    pnlShield.Visible = False
    iNumPlayers = spnNumPlayers.SelectedIndex + 2
    If chkNetGame.Checked Then StartService(NetConnect) Else New_Game
End Sub

& here is the Service_Destroy code:

B4X:
Sub Service_Destroy
    UDPSocket1.Close
    Tablet.RunMethod("release")
    tcpClient.Close
    tcpServer.Close
 
End Sub

I put a breakpoint on the "StopService(NetConnect)" line in btnStart_Click & another in the first line of Service_Destroy, & I can see that StopService(Netconnect) is being executed, however Service_Destroy never appears to execute.
 
Last edited:
Upvote 0

MaaMoz

Member
Licensed User
Longtime User
Can you upload your project (File - Export as zip)?

Hello Erel,
I know your reply to this post is old.
But I am looking into a problem of a service I cannot stop.
Long after I have used StopService(phoneService) and have closed the App, suddenly I get the message "Sorry but xxPlay has stopped" (xxPlay being the name of my App).
Info: I use the Service 'phoneService' to handle incoming Phone Calls and SMSs to Pause my xxPlayer.

I was wondering: What problem did you find upon receiving the project file you requested above?

Greetings MaaMoz
 
Upvote 0
Top