Android Question Service module doubts

angel

Member
Licensed User
Longtime User
Hello

I have a Start button and a Stop button, they initiate or stopping a service module each time X has to stop i just send a notice if you want to start the service module, if the timer is in the Main application module when is open but if this works in pause or closed ends when the service module start counting again. If the timer is in the service module does not work.

As I do to stop it when the Activity is paused or closed?
where I put the timer on the Main or the Service Module?
Can a service module stop or destroy the same?

MAIN module
B4X:
Sub btnStart_Click
    timer1.enabled=True
    StartService(Temporitzador)
End Sub

Sub btnStop_Click
    timer1.Enabled=False
    StopService(Temporitzador)
    CancelScheduledService(Temporitzador)
End Sub

Service Module Temporizador
B4X:
Sub Process_Globals
    Dim counter As Int
    Dim timer As Timer
End Sub

Sub Service_Create
    counter = 10
    timer.Initialize("timer",1000)
End Sub

Sub Service_Start (StartingIntent As Intent)
    timer2.Enabled = True
    StartServiceAt("", DateTime.Now + counter * DateTime.TicksPerSecond, True)
    End If
End Sub

Sub Service_Destroy

End Sub

Sub timer2_Tick
    counter = counter - 1
    If counter = 0 Then
        StopService("")
        CancelScheduledService("")
        timer2.Enabled=False
        ToastMessageShow("Stop temporitzador" ,True)
    End If
End Sub

if I remove the timer2.enabled = false works but I have another problem, if you start the Service Module and turn off the application when the service ends then restarts, how to solve this problem?

Thank you.
 
Last edited:

angel

Member
Licensed User
Longtime User
It's a timer countdown, when you finish the StartServiceAt have to stop the service, it works but when I close the application when the service ends restarts again
 
Upvote 0

angel

Member
Licensed User
Longtime User
removing this line when the application stops "pause" not operate the service module,
My intension is to continue with the application stop the service module
 
Upvote 0

angel

Member
Licensed User
Longtime User
Sorry I can not explain well
1. Start Service button start module
2. After one hour stop the service module and displays a message
3. It has to work with the open and closed application.

The duration may vary from service module, is calculated from two datetime.
The function is an alarm that alerts you after a while.
 
Upvote 0

angel

Member
Licensed User
Longtime User
Hello Erel

Post an example with images of log

In the example we have "Time to Start service" and "Time to Stop service" the difference is 10 seconds and the service is destroyed

Fails when: I close the application
"** Activity (main) Pause, UserClosed = false **" the service module reboots again, and the difference is 20 seconds when the service is destroyed

desire that the difference is always 10 seconds.

How to do it?

Thank you.
 

Attachments

  • log works.png
    log works.png
    12 KB · Views: 165
  • log does not work.png
    log does not work.png
    13.9 KB · Views: 142
  • Service module.zip
    7.7 KB · Views: 156
Upvote 0

angel

Member
Licensed User
Longtime User
Hello

works, this way for the service with the aplicacion closed, but not if it is correct

This code into Service_Start

B4X:
If StartingIntent.Flags >= 4 Then
        CancelScheduledService("")
        StopService("")
End If
 
Upvote 0
Top