Android Question Stopping a service

rayellam

Member
Licensed User
Longtime User
Hi All,
Newbie question...............
How do i stop a service created with StartServiceAt().

When i use StopService() in the main activity the service will stop and i exit the activity that called the service but after StartServicesAt time period the service restarts?

Main activity code
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Timer1 As Timer
      Timer1.Initialize("Timer1",1000)

End Sub
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim n As Notification
    Dim btnStartS As Button
    Dim btnStopS As Button
    Dim Label1 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("test")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
    StopService(Serial)
    Activity.Finish
End Sub
Sub Timer1_Tick
    'Update service counter value
      Label1.Text=Serial.Counter
End Sub
Sub btnStartS_Click
    'Start the service
    Timer1.Enabled=True
    StartService(Serial)
End Sub
Sub btnStopS_Click
    'Stop the service
    Timer1.Enabled=False
    StopService(Serial)
End Sub

Service module code
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim beep As Beeper
    Dim nSer As Notification
    Dim Counter As Int
End Sub
Sub Service_Create
    Counter=0
    nSer.Initialize
    beep.Initialize(100, 1000) '100 milliseconds, 1Khz
End Sub

Sub Service_Start (StartingIntent As Intent)
    'Start service evry x seconds
    'StartServiceAt("", DateTime.Now + 10 * DateTime.TicksPerSecond, True)
    'Service.StartForeground(1,nSer)
    StartServiceAt("", DateTime.Now + 3 * DateTime.TicksPerSecond, True)
   
    'Service notification
    nSer.Icon = "icon"
    nSer.Vibrate = False
    nSer.Sound = False
    nSer.SetInfo("MyApp","Service Running",Main)
    nSer.OnGoingEvent = False
    nSer.Light = True
    nSer.Notify(1)
    Counter=Counter+1 ' This counting will not work for long. If it is important it should be saved in a file instead of a variable.
   
    beep.beep
End Sub
Sub Service_Destroy
    nSer.Cancel(1)
End Sub

Thanks
Ray
 

rayellam

Member
Licensed User
Longtime User
I should have added that when i exit the app i want the service to be killed off and not restart untill i restart the app.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…