Android Question TIMER PROBLEMS

FabioG

Active Member
Licensed User
Longtime User
hello to everybody

I have a problem with the execution of three cascaded timer. these three timers work perfectly if the device is connected in usb pc or AC and then when you unplug the cable, the code is executed until the sub reconnection, everything else stop.
Basically it works like
screen OFFthe first part of the event timer that disconnects the connection after 60 seconds, the second timer waits 10 minutes and the third timer hangs up the connection for 60 seconds and then break off and start again from the first timer until another event occurs the screen ON
This code is part of a service that is always active without disconnecting it when the device is connected to, this code works perfectly
the timers are declared in Process Globals
Here is the code in use

B4X:
Sub phev_screenOff(IntentAsIntent)

  timerOn.Enabled=False

  IftimerOff.IsInitialized = FalseThen

  timerOff.Initialize("TimerOff", 60000)

  EndIf

  timerOff.Enabled=True

  LogWrite("SERVIZIO - SCREEN OFF "& DateTime.Time(DateTime.Now))

End Sub


Sub TimerOff_Tick

  timerOff.Enabled=False

  wifi.TurnWiFiOff

  data.TurnDataConnectionOff

  LogWrite("SERVIZIO Sub TimerOff_Tick- connessione staccata"& DateTime.Time(DateTime.Now))

  Riconnessione

End Sub


Sub Riconnessione

  timerOff.Enabled=False

  IftimerRic.IsInitialized = FalseThen

    timerRic.Initialize("TimerRic", 60000)

  EndIf

  Iftempo2 = 0Then

  timerRic.Enabled = False

    LogWrite("SERVIZIO Sub Riconnessione - riconnessione disattivata" & tempo2)

  Else

  timerRic.Enabled = True

  LogWrite("SERVIZIO Sub Riconnessione - Timer Riconnessione Start " & DateTime.Time(DateTime.Now))

  EndIf

End Sub


Sub timerRic_Tick

  timerRic.Enabled = False

  VerificaConnessioneOn

  IftimerUnMin.IsInitialized = FalseThen

  timerUnMin.Initialize("TimerUnMin", 60000)

  EndIf

  timerUnMin.Enabled = True

  LogWrite("SERVIZIO Sub TimerRiconnessione_Tick- TimerUnMin Start "& DateTime.Time(DateTime.Now))

End Sub


Sub timerUnMin_Tick

  timerUnMin.Enabled = False

  wifi.TurnWiFiOff

  data.TurnDataConnectionOff

  LogWrite("SERVIZIO Sub timerUnMin_Tick- TimerUnMin Stop "& DateTime.Time(DateTime.Now))

  Riconnessione

End Sub


Sub VerificaConnessioneOn

  wifi.TurnWiFiOn

  data.TurnDataConnectionOn

  LogWrite("SERVIZIO - sub verificaconnessioneON")

End Sub
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
You might have to schedule different services to start rather than using a timer.

B4X:
StartServiceAt(SERVICE-TO-START, datetime.now+60000, True)

You might be able to get the timer to work correctly if you start the service as a foreground process.
 
Upvote 0

FabioG

Active Member
Licensed User
Longtime User
You might have to schedule different services to start rather than using a timer.

B4X:
StartServiceAt(SERVICE-TO-START, datetime.now+60000, True)

You might be able to get the timer to work correctly if you start the service as a foreground process.


hi jack
I've already used service start but the problem is not solved. I thought maybe I could use a stop watch as the timer does not work?
I attach the service to facilitate the understanding

B4X:
#Region  Service Attributes 
#StartAtBoot: True
#End Region
Sub Process_Globals
Dim phev2 As PhoneEvents
Dim wifi, data As Toggle
Dim timerOff, timerRic, timerUnMin As Timer
Dim tempo2 As Int
Dim kvs4 As KeyValueStore
End Sub
Sub Service_Create
End Sub
Sub Service_Start (StartingIntent As Intent)
phev2.Initialize("phev2")
wifi.Initialize
data.Initialize
If kvs4.IsInitialized = False Then
kvs4.Initialize(File.DirRootExternal, "settings3gw")
End If
tempo2 = kvs4.GetSimple("ms")
StartServiceAt("RBEoff", DateTime.Now+1800000, True)
End Sub
Sub Service_Destroy
End Sub
Sub phev2_screenOff(Intent As Intent)
'If timerOff.IsInitialized = False Then
timerOff.Initialize("timerOff", 60000)
'End If
timerOff.Enabled = True
LogWrite("RBEoff - SCREEN OFF "& DateTime.Time(DateTime.Now))
End Sub
Sub timerOff_Tick
timerOff.Enabled = False
wifi.TurnWiFiOff
data.TurnDataConnectionOff
Riconnessione
LogWrite("RBEoff Sub TimerOff_Tick- connessione staccata"& DateTime.Time(DateTime.Now))
End Sub
Sub Riconnessione
timerOff.Enabled = False
  timerRic.Initialize("TimerRic", 60000)
If tempo2 = 0 Then
timerRic.Enabled = False
LogWrite("RBEoff Sub Riconnessione - riconnessione disattivata" & tempo2)
Else
timerRic.Enabled = True
LogWrite("RBEoff Sub Riconnessione - Timer Riconnessione Start " & DateTime.Time(DateTime.Now))
 End If
End Sub
Sub timerRic_Tick
timerRic.Enabled = False
VerificaConnessioneOn
If timerUnMin.IsInitialized = False Then
timerUnMin.Initialize("TimerUnMin", 60000)
End If
timerUnMin.Enabled = True
LogWrite("RBEoff Sub TimerRiconnessione_Tick- TimerUnMin Start "& DateTime.Time(DateTime.Now))
End Sub
Sub timerUnMin_Tick
timerUnMin.Enabled = False
wifi.TurnWiFiOff
data.TurnDataConnectionOff
LogWrite("RBEoff Sub timerUnMin_Tick- TimerUnMin Stop "& DateTime.Time(DateTime.Now))
Riconnessione
End Sub
Sub phev2_screenOn(Intent As Intent)
timerOff.Enabled = False
timerRic.Enabled = False
timerUnMin.Enabled = False
LogWrite("RBEoff - timer off stoppati")
End Sub
Sub VerificaConnessioneOn
wifi.TurnWiFiOn
data.TurnDataConnectionOn
LogWrite("RBEoff - sub verificaconnessioneON")
End Sub
 
Upvote 0
Top