Problem using GPS in a service module

edgar_ortiz

Active Member
Licensed User
Longtime User
I need to report every 5 minutes (it's a parameter) the GPS location... and if there is NO signal... just report the NO signal.

Everything works fine if I have signal... but if NO signal... the service just finish

Any help is welcome.

B4X:
'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 GPS1 As GPS
   Dim GPS_Str_Next_Start   As String
   Dim GPS_Intentos_Busqueda   As Int
End Sub
Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
   Dim lc_Current_Hour   As Int
   Dim lc_Next_Start   As Long
   '
   ' StartServiceAt("", DateTime.Now + 3 * DateTime.TicksPerMinute, False)
   lc_Next_Start         = DateTime.Now + Main.SysGPSRutaTiempo * DateTime.TicksPerMinute
   lc_Current_Hour         = DateTime.GetHour(DateTime.Now)
   GPS_Str_Next_Start   = Rutinas.Get_Format_Date(lc_Next_Start, Main.SysFomatoFecha)
   Log("Debug: Current Time: " & Rutinas.Get_Format_Date(DateTime.Now, Main.SysFomatoFecha))
   Log("Debug: Next Start: " & Rutinas.Get_Format_Date(lc_Next_Start, Main.SysFomatoFecha))
   '
   If Main.SysGPSRuta = "S" Then
      If lc_Current_Hour >= Main.SysGPSHoraInicio AND lc_Current_Hour <= Main.SysGPSHoraFinal Then
         ' schedule the next run in "N" minutes
         '
         StartServiceAt("", lc_Next_Start, True)         
         Log("Debug: Schedule to : " & Rutinas.Get_Format_Date(lc_Next_Start, Main.SysFomatoFecha))
      End If
   End If
   '
   Find_GPS_Data
   '
   Log("Debug: End of Service_Start")
End Sub

Sub Service_Destroy
   GPS1.Stop
   '
   Log("Debug: End Of Service (Service_Destroy)")
End Sub

Sub Find_GPS_Data
   Log ("Debug: Start of Find_GPS_Data")
   Dim Notification_Tel As Notification
   ' 
   GPS1.Initialize("GPS")
   '
   If GPS1.GPSEnabled = False Then
      Main.GlbGPSEstado   = "I"
      Main.GlbGPSLatitud   = ""
      Main.GlbGPSLongitud   = ""
      '
      ' Mandar mensaje de activar el GPS
      Notification_Tel.Initialize
      Notification_Tel.Icon      = "icon"
      Notification_Tel.SetInfo("GPS Desactivado", "Debe de activar el GPS", "") 
      Notification_Tel.AutoCancel   = True
      Notification_Tel.Notify(1)
      '
      Store_In_Data_Base
   Else
      Main.GlbGPSEstado   = "A"
      Log("Debug: Start using GPS")
      GPS1.Start( 0, 0) 'Listen to GPS with no filters.
      ' GPS1.Start (Main.SysGPSRutaTiempo * 900, 0)
   End If
   '
End Sub

Sub GPS_LocationChanged (Location1 As Location)
   '
   Log("Debug: Start of GPS_LocationChanged")
   GPS_Intentos_Busqueda   = GPS_Intentos_Busqueda + 1
   '
   If Location1.Accuracy > 2 Then      ' a little change
      Main.GlbGPSLatitud   = Location1.Latitude
      Main.GlbGPSLongitud   = Location1.Longitude
      '
      Store_In_Data_Base
      '
      Log("Debug: Location Changed")
   End If
   '
   If GPS_Intentos_Busqueda > 10 Then
      Log("Debug: NO GPS Signal")
      ToastMessageShow("NO GPS Signal... STOP the search", True)
      GPS1.Stop
      '
      Store_In_Data_Base
      '
   End If
End Sub

Sub Store_In_Data_Base
   Log("Debug: Data to Store: " & "^" & Rutinas.Get_Format_Date(DateTime.Now, Main.SysFomatoFecha) & "^, " & _
                        "^" & Main.GlbGPSLatitud & "^, " & _
                        "^" & Main.GlbGPSLongitud & "^, " & _
                        "^" & Main.GlbGPSEstado & "^, " & _
                        "^" & GPS_Str_Next_Start & "^)")
   '
   Dim lcSqlComando As String
   '
   lcSqlComando = "INSERT INTO sg_t_gps (fecha_hora, latitud, longitud, estado_gps, fecha_hora_envio) values (" & _
                        "^" & Rutinas.Get_Format_Date(DateTime.Now, Main.SysFomatoFecha) & "^, " & _
                        "^" & Main.GlbGPSLatitud & "^, " & _
                        "^" & Main.GlbGPSLongitud & "^, " & _
                        "^" & Main.GlbGPSEstado & "^, " & _
                        "^" & GPS_Str_Next_Start & "^)"
   lcSqlComando   = lcSqlComando.Replace("^", Main.GlbComillas)
   Main.DBFacturacion.ExecNonQuery(lcSqlComando)
   '
   Service_Destroy
End Sub

LOG REPORT:

HTML:
LogCat connected to: B4A-Bridge: samsung GT-S5830M-359971046757729
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
Installing file.
PackageAdded: package:facturacion.android
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (gps) Create **
** Service (gps) Start **
Debug: Current Time: 2012-10-13 22:46:22
Debug: Next Start: 2012-10-13 22:49:22
Debug: Schedule to : 2012-10-13 22:49:22
Debug: Start of Find_GPS_Data
Debug: Start using GPS
Debug: End of Service_Start
** Activity (main) Pause, UserClosed = false **
** Service (gps) Start **
Debug: Current Time: 2012-10-13 22:49:22
Debug: Next Start: 2012-10-13 22:52:22
Debug: Schedule to : 2012-10-13 22:52:22
Debug: Start of Find_GPS_Data
Debug: Start using GPS
Debug: End of Service_Start
** Service (gps) Start **
Debug: Current Time: 2012-10-13 22:52:22
Debug: Next Start: 2012-10-13 22:55:22
Debug: Schedule to : 2012-10-13 22:55:22
Debug: Start of Find_GPS_Data
Debug: Start using GPS
Debug: End of Service_Start
 
Top