Android Question Stopping a service

Graeme Tacon

Member
Licensed User
Longtime User
Hi,

If you start a service with an intent using 'StartServiceAt', how do you cancel it ?

See attached - clicking the 'Schedule' buttons performs 3 startserviceat, with the scheduled times 5 seconds apart. Hitting the 'Cancel' button issues a Cancelscheduledservice command and a stopservice command, but the service still fires.

What have I done wrong ?

Thanks
Graeme
 

Attachments

  • servicedemo.zip
    7.8 KB · Views: 124

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that each call to StartServiceAt will remove previous schedules.

StartServiceAt expects a service not an intent. I've tested it with this code and it works as expected:
B4X:
Sub schedule_service(secs As Int)
   Dim x As Long = DateTime.Now + (secs * DateTime.TicksPerSecond)
     StartServiceAt(testservice, x, True)
End Sub
 
Upvote 0

Graeme Tacon

Member
Licensed User
Longtime User
Thanks for the reply Erel

What I was trying to do in my app was just have 1 service which had multiple functions, rather than have several small services.

By using startserviceat with an intent (which I'd picked up on another post), I could achieve that, especially as you can do multiple calls which don't remove previous schedules. The issue is that I don't know how you can remove one that has been scheduled before it reaches the scheduled time.


I guess I will have to revert to having multiple services.
 
Upvote 0
Top