How to integrate a timer to the gps?

marbellapc

Member
Licensed User
Longtime User
Good, I'll ask a question, I have this code and I put a timer every 10 or 15 seconds to send the GPS position WebView_url

Sorry for my English, I'm a translator online
B4X:
'
' APPLICATION NAME: Track GPS
'          VERSION: beta 2012
'      DESCRIPTION: GPS positioning system.
'           DESIGN: MarbellaPC
'      PROGRAMMING: Marbellapc, based tutorials Basic4Android
'              URL: http://www.marbellapc.com
'            EMAIL: [email protected]
'         LANGUAGE: Spanish
'
Sub Process_Globals ' ---------------------------------------------------------------------- Declaramos los procesos Globales.
   Dim GPS1                 As GPS
   Dim lon, lat             As String    
' ------------------------------------------------------------------------------------------ Tenemos que Activar la Libreria NET para que funcione.   
   'Dim Mail                 As SMTP 
   Dim Timer1               As Timer   
End Sub

Sub Globals ' ------------------------------------------------------------------------------ Declaramos los Objetos. 
   Dim lblLon               As Label
   Dim lblLat               As Label
   Dim lblSpeed             As Label
   Dim lblSatellites        As Label   
   Dim Label_satelites      As Label
   Dim WebView_info         As WebView
' ----------------------------------------------------------- Declaramos la orden URL.   
   Dim url                  As PhoneIntents ' ----------- Tenemos que marcar la libreria "Phone" para activar "PhoneIntents".   
   Dim Button_atras         As Button
   Dim CheckBox_GPS         As CheckBox
   Dim CheckBox_Segundo     As CheckBox
   Dim ToggleButton_GPS     As ToggleButton
   Dim WebView_url          As WebView
' ------------------------------------------------------------------------------------------ Declaramos la orden URL.   
   Dim url                  As PhoneIntents ' ----------- Tenemos que marcar la libreria "Phone" para activar "PhoneIntents".   
   Dim ToggleButton_url     As ToggleButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      GPS1.Initialize("GPS")
   End If
   Activity.LoadLayout("gps_status")
' ----------------------------------------------------------- Declaramos los botones para el Menu.   
   Activity.AddMenuItem2("Salir",       "Salir",       LoadBitmap(File.DirAssets,"logout.png"))
   Activity.AddMenuItem2("Opciones",    "Opciones",    LoadBitmap(File.DirAssets,"opciones.png"))   
   Activity.AddMenuItem2("Información", "Informacion", LoadBitmap(File.DirAssets,"varios.png"))
' ----------------------------------------------------------- Declaramos el TIMER.      
   Timer1.Initialize("Timer1", 5000) ' 1000 = 1 Segundo
End Sub

' ------------------------------------------------------------------------------------------ La Sentencia TIMER.
Sub Timer1_Tick
   'Handle tick events
    Timer1.Enabled = False
End Sub

Sub Activity_Resume
   If GPS1.GPSEnabled = False Then
      ToastMessageShow("Encienda el dispositivo GPS.", True)
' ----------------------------------------------------------- Se abrirá la pantalla de configuración correspondiente.      
      StartActivity(GPS1.LocationSettingsIntent) 
   Else
' ----------------------------------------------------------- Escucha GPS sin filtros.   
      GPS1.Start(0, 0)
   End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   GPS1.Stop
   Timer1.Enabled=False
End Sub

' ------------------------------------------------------------------------------------------ Evitamos que cierre la aplicacion al pulsar boton atras.
Sub Activity_KeyPress (KeyCode As Int) As Boolean                     
   Dim Answ As Int
    Select KeyCode
    Case KeyCodes.KEYCODE_BACK
    Answ = Msgbox2("¿Realmente desea cerrar la aplicación?", "A T E N C I O N", "Cerrar", "", "Cancelar", Null) 
    If Answ = DialogResponse.NEGATIVE Then   
' ----------------------------------------------------------- Llamamos para remover la pantalla activa.
   'RemoveViews   
' ----------------------------------------------------------- Llamamos a la pantalla principal.      
    'Activity.LoadLayout("gps_status")   
      Return True                                             
   Else
      Return False
   End If    
  End Select
End Sub

Sub RemoveViews
 Dim i As Int
 For i=Activity.NumberOfViews-1 To 0 Step -1
     Activity.RemoveViewAt(i)
 Next
End Sub

Sub GPS_LocationChanged (Location1 As Location)
    Dim GPS_Speed As String
       GPS_Speed = 3.6 ' ----------------------------------------------------------------- Velocidad: 1 m/s equivalen a 3.6 Km/h.   
      Speed = location1.Speed * GPS_Speed ' --------------------------------------------- Multiplicamos m/s por los Kilometros hora.  
   lblLat.Text   = "    " & Location1.ConvertToSeconds(Location1.Latitude) '-------------- Latitud en Decimal.
   lblLon.Text   = "    " & Location1.ConvertToSeconds(Location1.Longitude) ' ------------ Longitud en Decimal.
   lblSpeed.Text = "    " & Speed & " Km/h" 
' ------------------------------------------------------------------------------------------ INICIO: Enviar datos via web.
    WebView_url.LoadUrl("http://maps.google.es/maps?hl=es&ll="&Location1.Latitude&","&Location1.Longitude)
' ------------------------------------------------------------------------------------------ Lugar para poner el TIMER.
    Timer1.Enabled = True    
' ------------------------------------------------------------------------------------------ INICIO: Enviar coordenadas por correo.   
   'lon = Location1.ConvertToSeconds(Location1.Longitude)    
    'lat = Location1.ConvertToSeconds(Location1.Latitude)    
    'Mail.Initialize("smtp.gmail.com", 465, "Correo Emisor", "Clave", "Mail")    
    'Mail.UseSSL  = True     
    'Mail.To.Add("Correo Destinatario")    
    'Mail.Subject = "MENSAJE DE ALERTA"    
    'Mail.Body    = "La localizacion del Terminal +34xxx-xxx-xxx es Longitud: "& Location1.ConvertToSeconds(Location1.Longitude)&" y Latitud: "& Location1.ConvertToSeconds(Location1.Latitude)    
    'Mail.Send 
' ------------------------------------------------------------------------------------------ FINAL: Enviar coordenadas por correo.   
End Sub

Sub GPS_UserEnabled (Enabled As Boolean)
   ToastMessageShow("Dispositivo GPS: " & Enabled, True)
End Sub

Sub GPS_GpsStatus (Satellites As List)
   lblSatellites.Text     = " Listado de Satelites:" & CRLF
   For i = 0 To Satellites.Size - 1
      Dim Satellite As GPSSatellite
      Satellite          = Satellites.Get(i)
      lblSatellites.Text = lblSatellites.Text & CRLF & "  " & Satellite.Prn & ": " & _
         " " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _ 
         & " " & Satellite.Elevation
   Next
End Sub

Sub Salir_Click
Select Msgbox2("¿Realmente desea cerrar la aplicación?", "A T E N C I O N", "Cerrar", "Cancelar", "", Null)
' ------------------------------------------------------------------------------------------ Sin uso.
  Case DialogResponse.NEGATIVE  
' ------------------------------------------------------------------------------------------ Cancelamos y volvemos a la pantalla.
  Case DialogResponse.CANCEL
' ------------------------------------------------------------------------------------------ Cerramos la aplicacion.
  Case DialogResponse.POSITIVE
    Activity.Finish
End Select    
End Sub

Sub Opciones_click
' ----------------------------------------------------------- Llamamos para remover la pantalla activa.
   RemoveViews   
' ----------------------------------------------------------- Llamamos a la pantalla.   
    Activity.LoadLayout("opciones")
End Sub

Sub Informacion_click
' ----------------------------------------------------------- Llamamos para remover la pantalla activa.
   RemoveViews   
' ----------------------------------------------------------- Llamamos a la pantalla.   
    Activity.LoadLayout("informacion")
   Dim url_info As String
    url_info = File.GetText(File.DirAssets, "informacion.htm") ' - Cargamos el HTML de la pagina a mostrar.
   WebView_info.LoadHtml(url_info)
End Sub

Sub Button_atras_Click
' ----------------------------------------------------------- Llamamos para remover la pantalla activa.
   RemoveViews   
' ----------------------------------------------------------- Llamamos a la pantalla.   
    Activity.LoadLayout("gps_status")
End Sub

' ------------------------------------------------------------------------------------------ Boton Encendido/Pausado del GPS.
Sub ToggleButton_GPS_CheckedChange(Checked As Boolean)
   If Checked = True Then
   ToastMessageShow("Dispositivo GPS: Iniciado", True)
   GPS1.Start(0, 0)
    Else    
   ToastMessageShow("Dispositivo GPS: Pausado", True)
   GPS1.Stop
    End If
End Sub

I have followed tutorials on how to integrate the timer but without success, I've also looked at the wiki and I have seen this post but just do not get to run the timer.

If you can help me out, I would appreciate greatly.
Greetings and thanks
 

bluedude

Well-Known Member
Licensed User
Longtime User
Timer?

Not sure why you want to use a timer, seems like a bad idea with GPS. The GPS already has events and it would be much better to use the location changed event to post data to an url. You can provide the GPS.Start with parameters for movement etc. You could even check the movement of a user between position updates. No movement is no post, saves a lot of bandwidth and trouble.

If you want to insist on using a timer I would create a GPS service.
 
Upvote 0

Jost aus Soest

Active Member
Licensed User
Longtime User
Yes, don't use a timer!

In GPS_LocationChanged you could check for the time of the last update (using DateTime.Now) to execute some code only every n seconds.
 
Upvote 0

marbellapc

Member
Licensed User
Longtime User
Ok, thank you very much to both of the answers, using the timer, it is because he believed there had to be so ...

I am very new to the Basic4Android and there are things that still do not understand, but now thanks to your answers, I will put to research "DateTime.Now" you did not know.

Again, forgive my English and thank you very much to both.

Regards
 
Upvote 0

marbellapc

Member
Licensed User
Longtime User
Good morning, after several days trying to understand how the DateTime.Now I have to say I do not get anything clear on how to integrate it into the application, if they could help me out I would appreciate, as I have been blocked.

Do not quite understand how I have to create the code to work and send from time to time function.

If you could take a simple example, I would appreciate greatly.

Greetings and thanks in advance, and again forgive me for my English
 
Upvote 0
Top