Android Question function timer

westingenieria

Active Member
Licensed User
Longtime User
hi everyone,

I have a question relation with function timer, if it is runable or asyntask , anyone to know ???

the best regards.
 

DonManfred

Expert
Licensed User
Longtime User
What are you trying to archieve?
In my understanding a Timer is not a AsyncTask and also not a runable.
 
Upvote 0

westingenieria

Active Member
Licensed User
Longtime User
@DonManfred because i have too much timer in my application, and then if it is service, the service in Android take the main thread of application, and asyntask create a new thread in the background.

the best regard
 
Upvote 0

westingenieria

Active Member
Licensed User
Longtime User
because I have several timer on the main thread, for intance one timer is for refresh map each two seconds on the main thread. sometimes it is very slow for the application for devices with slow resources.
 
Upvote 0

westingenieria

Active Member
Licensed User
Longtime User
this is my timer for refresh my position map each two seconds.

B4X:
Sub tmMapa_Tick
    Dim cp                 As CameraPosition
    Dim lat             As Float
    Dim lon             As Float
    Dim distancia         As Double
   
    'Log("punto actual: "& posLatitud &" || "&posLongitud &" || "&posDireccion)
   
'''    posLatitud             = -39.829855
'''    posLongitud         = -73.238061
    posLatitud = svcGps.gdLatitud
    posLongitud = svcGps.gdLongitud
    posDireccion = svcGps.giDireccion
    posVelocidad = svcGps.giVelocidad
   
    tmMapa.Enabled = False
   
'    Log("punto actual: "& posLatitud &" || "&posLongitud &" || "&posDireccion &" || "&posVelocidad)

    If posVelocidad > 15 Then
        zoom= gmap.CameraPosition.zoom+17
    Else
        zoom = 15
    End If
   
   
    If movil = True Then
        If markerActual.IsInitialized Then markerActual.Remove
       
        If posVelocidad <> 0 Then
            If posDireccion <= 22 Or posDireccion > 337 Then
                cp.Initialize2(posLatitud,posLongitud,zoom,0,90)
                'gmap.AnimateCamera(cp)
                gmap.MoveCamera(cp)
            Else If posDireccion <= 67 And posDireccion > 22 Then
                    cp.Initialize2(posLatitud,posLongitud,zoom,44,90)
                    'gmap.AnimateCamera(cp)
                    gmap.MoveCamera(cp)
            Else If posDireccion <= 112 And posDireccion > 67 Then
                    cp.Initialize2(posLatitud,posLongitud,zoom,89,90)
                    'gmap.AnimateCamera(cp)
                    gmap.MoveCamera(cp)
            Else If posDireccion <= 157 And posDireccion > 112 Then
                    cp.Initialize2(posLatitud,posLongitud,zoom,134,90)
                    'gmap.AnimateCamera(cp)
                    gmap.MoveCamera(cp)
            Else If posDireccion <= 202 And posDireccion > 157 Then
                    cp.Initialize2(posLatitud,posLongitud,zoom,180,90)
                    'gmap.AnimateCamera(cp)
                    gmap.MoveCamera(cp)
            Else If posDireccion <= 247 And posDireccion > 202 Then
                    cp.Initialize2(posLatitud,posLongitud,zoom,224,90)
                    'gmap.AnimateCamera(cp)
                    gmap.MoveCamera(cp)
            Else If posDireccion <= 292 And posDireccion > 247 Then
                    cp.Initialize2(posLatitud,posLongitud,zoom,270,90)
                    'gmap.AnimateCamera(cp)
                    gmap.MoveCamera(cp)
            Else If posDireccion <= 337 And posDireccion > 292 Then
                    cp.Initialize2(posLatitud,posLongitud,zoom,315,90)
                    'gmap.AnimateCamera(cp)
                    gmap.MoveCamera(cp)
            End If
            Select flecha
                Case "ORIGEN"
                    markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imONorte)
                Case "DESTINO"
                    markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imDNorte)
                Case Else
                    markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imSNorte)
            End Select
        Else
            markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imDetenido)
            cp.Initialize(posLatitud,posLongitud, zoom)
            'gmap.AnimateCamera(cp)
            gmap.MoveCamera(cp)
        End If
   
    Else
            If markerActual.IsInitialized Then markerActual.Remove
           
       
        If posVelocidad <> 0 Then
            DoEvents
            Select flecha
                Case "ORIGEN"
                    If posDireccion <= 22 Or posDireccion > 337 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imONorte)
                    If posDireccion <= 67 And posDireccion > 22 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imONorEste)
                    If posDireccion <= 112 And posDireccion > 67 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imOEste)
                    If posDireccion <= 157 And posDireccion > 112 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imOSurEste)
                    If posDireccion <= 202 And posDireccion > 157 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imOSur)
                    If posDireccion <= 247 And posDireccion > 202 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imOSurOeste)
                    If posDireccion <= 292 And posDireccion > 247 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imOOeste)
                    If posDireccion <= 337 And posDireccion > 292 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imONorOeste)
       
                Case "DESTINO"
                    If posDireccion <= 22 Or posDireccion > 337 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imDNorte)
                    If posDireccion <= 67 And posDireccion > 22 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imDNorEste)
                    If posDireccion <= 112 And posDireccion > 67 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imDEste)
                    If posDireccion <= 157 And posDireccion > 112 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imDSurEste)
                    If posDireccion <= 202 And posDireccion > 157 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imDSur)
                    If posDireccion <= 247 And posDireccion > 202 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imDSurOeste)
                    If posDireccion <= 292 And posDireccion > 247 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imDOeste)
                    If posDireccion <= 337 And posDireccion > 292 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imDNorOeste)
       
                Case Else 'SIN GIRO
                    If posDireccion <= 22 Or posDireccion > 337 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imSNorte)
                    If posDireccion <= 67 And posDireccion > 22 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imSNorEste)
                    If posDireccion <= 112 And posDireccion > 67 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imSEste)
                    If posDireccion <= 157 And posDireccion > 112 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imSSurEste)
                    If posDireccion <= 202 And posDireccion > 157 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imSSur)
                    If posDireccion <= 247 And posDireccion > 202 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imSSurOeste)
                    If posDireccion <= 292 And posDireccion > 247 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imSOeste)
                    If posDireccion <= 337 And posDireccion > 292 Then markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imSNorOeste)
            End Select
        Else
            markerActual = gmap.AddMarker3(posLatitud, posLongitud, "Usted está aquí", imDetenido)
        End If
       
        lat = gmap.CameraPosition.Target.Latitude
        lon = gmap.CameraPosition.Target.Longitude
        cp.Initialize2(lat,lon,zoom,0,90)
        'gmap.AnimateCamera(cp)
        gmap.MoveCamera(cp)
     End If
    lblVelocidadR.Text = posVelocidad & " km/h"
       
        'LogColor("lblVelocidadR: " & lblVelocidadR.Text, Colors.Blue)
       
        If lblEstadoGiro.Text = "" Or lblEstadoGiro.text = "Sin giro" Or lblEstadoGiro.text = "Terminado" Then
             lblETA.Text             = "N/D"
            lblDistancia.Text         = "N/D"
            lblETAR.Text             = "N/D"
            lblDistanciaR.Text         = "N/D"
        Else If lblEstadoGiro.text = "Hacia origen" Then
             Try
                If posLatitud <> 0 And posLongitud <> 0 Then
                    distancia = Round2((Round(Comun.revisaPuntoPunto2(posLatitud,posLongitud,sX_ORIGEN.val,sY_ORIGEN.val)) / 1000),2)
                    lblDistancia.Text             = distancia & " Kms"
                    lblDistanciaR.Text             = distancia & " kms"
                     lblDistancia.Text             = lblDistancia.Text.Replace(".",",")
                    lblDistanciaR.Text             = lblDistanciaR.Text.Replace(".",",")
                Else
                    lblDistancia.Text         = "N/D"
                    lblDistanciaR.Text         = "N/D"
                End If
               
                If posVelocidad <> 0 And  posLatitud <> 0 And posLongitud <> 0  Then
                    lblETA.Text     = Round((distancia / posVelocidad) * 60) & " Min."
                    lblETAR.Text     = Round((distancia / posVelocidad) * 60) & " min"
                Else
                    lblETA.Text     = "N/D"
                    lblETAR.Text     = "N/D"
                End If
            Catch
                'Log("catch tmMapa_Tick  Main   " & LastException)
                lblETA.Text     = "N/D"
                lblETAR.Text    = "N/D"
                tmMapa.Enabled = True
            End Try
        Else If lblEstadoGiro.text = "En origen" Or lblEstadoGiro.text = "En destino" Then
             lblETA.Text         = "0 Minutos"
            lblDistancia.Text     = "0 KM"
            lblETAR.Text         = "0 min"
            lblDistanciaR.Text     = "0 km"
        Else If lblEstadoGiro.text = "Hacia destino" Then
             Try
                If posLatitud <> 0 And posLongitud <> 0 Then
                    distancia = Round2((Round(Comun.revisaPuntoPunto2(posLatitud,posLongitud,sX_DESTINO.val,sY_DESTINO.val)) / 1000),2)
                    lblDistancia.Text         = distancia & " Kms"
                    lblDistanciaR.Text         = distancia & " kms"
                    lblDistancia.Text         = lblDistancia.Text.Replace(".",",")
                    lblDistanciaR.Text         = lblDistanciaR.Text.Replace(".",",")
                Else
                    lblDistancia.Text         = "N/D"
                    lblDistanciaR.Text         = "N/D"
                End If
               
                If posVelocidad <> 0 And  posLatitud <> 0 And posLongitud <> 0 Then
                    lblETA.Text     = Round((distancia/posVelocidad)*60) & " Min."
                    lblETAR.Text     = Round((distancia/posVelocidad)*60) & " min"
                Else
                    lblETA.Text     = "N/D"
                    lblETAR.Text     = "N/D"
                End If
            Catch
                Log("catch tmMapa_Tick 1  Main   " & LastException)
                lblETA.Text     = "N/D"
                lblETAR.Text     = "N/D"
                tmMapa.Enabled = True
            End Try
         End If
    tmMapa.Enabled = True
    DoEvents
End Sub

the best regards.
 
Upvote 0

westingenieria

Active Member
Licensed User
Longtime User
hi at all,

but when i take off the functions doEvents, the maps not refreshing correctly, why happening this behavior ????
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
If it takes more than 2 seconds to refresh the map (very likely in most circumstances based on my experience, but it depends on your internet speed and the load on the device) your timer will fire again before the previous map has loaded. That will cause the current web page loading to abort and start again and you are never going to get out of the hole.
I had the same issue with my tracking app and the way to solve it it not to fire the timer every two seconds, but wait until the page is loaded, then fire a shorter timer (maybe 300 or 500mS) before reloading the page again. This will avoid trying to reload the page before it's loaded.

Try to set the timer to 5 or 10 seconds to estimate how long it takes to load the page.

But again, the issue is that the time it takes to load the page can be highly variable, so you do not want a fixed timer. Use the PageLoaded PageFinished event to fire a timer, then reload the page after that timer elapses.
 
Last edited:
Upvote 0

westingenieria

Active Member
Licensed User
Longtime User
okey, I will try change my timer at 3 seconds for see the behaivor, althougth I disable timer in the beginning of it, and then enable the timer again, when the code finishing of execute.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
okey, I will try change my timer at 3 seconds for see the behaivor, althougth I disable timer in the beginning of it, and then enable the timer again, when the code finishing of execute.
B4X:
sub mytimer_tick
  mytimer.enabled = false
  sleep(0) ' Propbably necessary
  ' do your work
 '
  mytimer.enabled = true
end sub
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
okey, I will try change my timer at 3 seconds for see the behaivor, althougth I disable timer in the beginning of it, and then enable the timer again, when the code finishing of execute.

The page is not necessarily loaded when the code "finishes to execute". Your code launches the WebView but does not wait for it to complete before executing the code below, so even though your code "finishes", the webview is not done. The only way to know when the WebView is done is to use the PageFinished event (not PageLoaded as incorrectly called in my previous post).

It is my experience that the more markers and other custom objects you have on the map, the longer it takes to reload (because it cannot be served from cache), 3 seconds would probably not be enough, particularly on a slower device with average network connection.
 
Last edited:
Upvote 0
Top