How long can be a timer in a service?

fanfalveto

Active Member
Licensed User
Longtime User
Hi,at the first time sorry for my english.
I´ve this code:


'Service module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim inici As Timer
Dim tiempo As Timer
Dim temp As Timer
Dim alea As Int
Dim n As Notification
'Dim vibrar As Int
Dim dat As leerdatos
Dim nombre As String
Dim hora1 As String
Dim hora2 As String
Dim hora3 As String
Dim hora4 As String
Dim hora As Int
Dim sonido As String
Dim inth1 As Int
Dim inth2 As Int
Dim inth3 As Int
Dim inth4 As Int
Dim i As Int
Dim ct1 As CustomToast
Dim alerta As Bitmap
Dim hc As HttpClient
Dim posimagen As Int
End Sub
Sub Service_Create
'tiempo.Enabled=False
'ToastMessageShow("crea al servicio con vibrar "&vibrar,False)
n.Initialize
n.Icon="icon"
inici.Initialize("inici1",100)
inici.Enabled=True
End Sub

Sub Service_Start (StartingIntent As Intent)
nombre=dat.leernom
sonido=dat.sonidodat
hora1=dat.h1a
hora2=dat.h2a
hora3=dat.h3a
hora4=dat.h4a
inth1=hora1
inth2=hora2
inth3=hora3
inth4=hora4
posimagen=Rnd(1,50)
hora=DateTime.GetHour(DateTime.Now)
ct1.Initialize
alerta.Initialize(File.DirAssets,"alarma.jpg")
idmovil.vibras=0
'ToastMessageShow("sonido: "&sonido,False)
If idmovil.soni=0 Then
n.Sound=False
Else
n.Sound=True
End If
n.SetInfo(nombre, "A fichar " , idmovil)
n.Vibrate=True
'Change Main (above) to "" if this code is in the main module.
'n.Notify(1)
'StartServiceAt("",DateTime.Now+alea,True) '
ToastMessageShow(" servicio iniciado ",False)
End Sub

Sub inici1_Tick
'ToastMessageShow("inicio " &inth1& " " &"hora del movil"& hora,False)
If inth1<=hora AND hora<inth2 Then
alea=Rnd(1,5)
temp.Initialize("temp1",5000*alea)
temp.Enabled=True
Else If inth3<=hora AND hora<inth4 Then
alea=Rnd(3,10)
temp.Initialize("temp1",5000*alea)
temp.Enabled=True
End If
End Sub
Sub temp1_Tick
Service.StartForeground(1,n)
tiempo.Initialize("tiempo1",1000)
tiempo.Enabled=True
'StartActivity(idmovil)
'n.Notify(1)
'n.AutoCancel=True
ct1.ShowBitmap( "",1000,Gravity.FILL,10,10,alerta)
'tiempo1_Tick
i=i+1
End Sub
Sub tiempo1_Tick
i=i+1
If idmovil.vibras=1 Then
cancelvibrate
'n.Cancel(1)
Else If i=10 Then
cancelvibrate
Else
n.Vibrate=True
Vibrate(500,200)

End If
End Sub

Sub Vibrate(On As Long, Off As Long)
Dim r As Reflector
r.Target = r.GetContext
r.Target = r.RunMethod2("getSystemService", "vibrator", "java.lang.String")
Dim pattern(2) As Long
pattern(0) = On
pattern(1) = Off
r.RunMethod4("vibrate", Array As Object(pattern, 0), Array As String("[J", "java.lang.int"))
End Sub

Sub cancelvibrate
Dim r As Reflector
r.Target = r.GetContext
r.Target = r.RunMethod2("getSystemService", "vibrator", "java.lang.String")
r.RunMethod("cancel")
tiempo.Enabled=False
' ToastMessageShow("vibracion: "& i,False)
i=0
ExecuteRemoteQuery("http://192.168.1.33/android.php?hora1="& hora,1)
Service.StopForeground(1)
n.Cancel(1)
End Sub
Sub ExecuteRemoteQuery(Query As String, TaskId As Int)
Dim req As HttpRequest
req.InitializePost2(Query, Query.GetBytes("UTF8"))
ToastMessageShow(Query,True)
hc.Execute(req, TaskId)
End Sub
Sub Service_Destroy
ToastMessageShow("servicio detenido",False)
End Sub

Run ok if timer is so small,but when i put 30 o more minutes then service stop.
I call the service from activity,first estop previous service an after call again.
Please can hellp me.
Thanks
 

fanfalveto

Active Member
Licensed User
Longtime User
Thanks,i have seen the post about services,but i do not know if the time of Timer is very long causes stop the service.
if timer is only for a few minutes works right,but i need the time into service start until 1 or 3 hours.
i can call with StartServiceAt,from the activity but for this the activity is open and i need the activity be closed.
Sorry for my english,I hope you understand what I mean
 
Upvote 0

fanfalveto

Active Member
Licensed User
Longtime User
Thanks, I will try to prove StartServiceAt ("", DateTime.Now +30 * 1000, True).
To see if the service calls itself after 30 seconds
 
Upvote 0

fanfalveto

Active Member
Licensed User
Longtime User
solved

In the end I got that service is not interrupted.
I call from the activity and successive chained Timer, until that last between one and two hours.
Then show a notification, the service closes and restarts.
In this way always in operation except for 10 milliseconds.
Memory consumption is 11 MB
Thanks for the help
 
Upvote 0

jeronimovilar2

Member
Licensed User
Longtime User
:(

I tryed StartServiceAt ("", DateTime.Now +1, True), but when i´ll to home or the device is blocked the program (timer) is paused. what can i do?

I need a program similar to the clock that not stop/pause when the device is blocked.

:sign0085:
 
Upvote 0
Top