Android Question How to stop GPS in a servie module?

georgelbs

Member
Licensed User
Longtime User
The GPS once started stay On all the time, consuming battery, I only need start every 30 min. write the DB and stop it until next start.

Here is me code

B4X:
#Region Module Attributes
    #StartAtBoot: true
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

'Service module
Sub Process_Globals
Dim Notification1 As Notification
Dim GPS1 As GPS
Dim SQL As SQL
Dim Lat As String 
Dim Lon As String
Dim phoneInfo As PhoneId
Dim deviceID, SQL2 As String
Dim now As Long                
Dim edtdate, edttime, edtdatetime As String
End Sub

Sub Service_Create
GPS1.Initialize("GPS")
If GPS1.GPSEnabled = False Then
   ToastMessageShow("Encienda el GPS por favor", True)  
   StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
End If
SQL.Initialize(File.DirRootExternal, "geo_data.db", False)
Notification1.Initialize
Notification1.Icon = "icon"
Notification1.Vibrate = False
Notification1.AutoCancel = True
Notification1.Sound = False
Notification1.SetInfo("SAMI-GEO", "Conectado", Main)
Service.StartForeground(1, Notification1)
End Sub


Sub Service_Start
StartServiceAt("", DateTime.now + 30 * DateTime.TicksPerMinute, True)
If GPS1.GPSEnabled = False Then
   Toggle.TurnGPSOn
   If Toggle.GPS = False Then
      ToastMessageShow("Encienda el GPS por favor", True)
      StartActivity(GPS1.LocationSettingsIntent)
   End If  
Else
   GPS1.Start(0, 0)
End If
createRecord
End Sub

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

Sub GPS_LocationChanged (Location1 As Location)
Lat =  Location1.Latitude
Lon =  Location1.Longitude
End Sub

Sub GPS_UserEnabled (Enabled As Boolean)
    ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub

Sub createRecord
If GPS1.GPSEnabled = False Then
   ToastMessageShow("Encienda el GPS por favor", True)
   StartActivity(GPS1.LocationSettingsIntent) 
End If
If Lat <> "" Then
   DateTime.DateFormat = "yyyy/MM/dd"  
   now = DateTime.now
   edtdate = DateTime.Date(now)
   edttime= DateTime.Time(now)  
   deviceID = phoneInfo.GetDeviceId
   Notification1.SetInfo("SAMI_GEO", Lat & " " & Lon, Main)
   Notification1.Notify(1)
   SQL.ExecNonQuery("INSERT INTO geo_data (fecha, hora, latitud, longitud, phonedID, transmitido) VALUES ('" & edtdate & "','" & edttime & "','" & Lat & "','" & Lon & "','" & deviceID & "','N')")
End If
End Sub
 

georgelbs

Member
Licensed User
Longtime User
Thanks Erel, one more question, if call StopService(Me), the service will be started again (
StartServiceAt("", DateTime.now + 30 * DateTime.TicksPerMinute, True))?
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
I might be a bit late, but try CancelScheduledService from outside the service. Any action programmed in the destroy sub is useless when the next restart of the service is already scheduled. Destroy is executed, but the service will restart as previuosly scheduled. The tutorial does not elaborate on this issue.
 
Upvote 0
Top