I am trying to optimize battery life on a GPS enabled app, I've moved the Fused Location Provider into a service and am starting the service when a current location update is desired. When the app is idle, a location update every hour is sufficient, but when the user is actively using the app, more frequent updates are needed (example every 30 seconds).
It seems however that the GPS polling is continuous regardless of the service being active and the battery drain still significant with this approach.
I've tried destroying the service at the end of the service_create, but that prevents the LocationChanged event from ever firing. It seems that FLP.Disconnect has the same effect.
I also tried switching to .Priority.PRIORITY_BALANCED_POWER_ACCURACY however, the HIGH accuracy is required for the app.
Any advice on improving the battery life while using the GPS is appreciated. Here is the service code that I am currently using.
Thanks,
Gentry
It seems however that the GPS polling is continuous regardless of the service being active and the battery drain still significant with this approach.
I've tried destroying the service at the end of the service_create, but that prevents the LocationChanged event from ever firing. It seems that FLP.Disconnect has the same effect.
I also tried switching to .Priority.PRIORITY_BALANCED_POWER_ACCURACY however, the HIGH accuracy is required for the app.
Any advice on improving the battery life while using the GPS is appreciated. Here is the service code that I am currently using.
Thanks,
Gentry
B4X:
#Region Service Attributes
#StartAtBoot: False
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim haslocationchanged As Boolean
Dim NTCFrequency as int
Dim FusedLocationProvider1 As FusedLocationProvider
Dim playmodeOn as Boolean
End Sub
Sub Service_Create
Log("loc - Service_Create")
NTCFrequency = 30
playmodeOn = true
FusedLocationProvider1.Initialize("FusedLocationProvider1")
notifycount = 0
haslocationchanged = True
DateTime.DateFormat = DateTime.DeviceDefaultDateFormat
End Sub
Sub Service_Start (StartingIntent As Intent)
Log("loc - Service_Start")
Try
If FusedLocationProvider1.IsConnected = False Then
FusedLocationProvider1.Connect
End If
Dim CheckPeriod As Period
CheckPeriod.Seconds = NCTFrequency
If boolPlayModeOn Then 'check more frequently in play mode, every hour if not in play mode
Log("play mode on")
StartServiceAt(Me, DateTime.Now + (NCTFrequency * DateTime.TicksPerSecond),True)
Else
Log("play mode off")
StartServiceAt(Me,DateTime.Now + (60 * DateTime.TicksPerMinute),True)
End If
'FusedLocationProvider1.Disconnect 'tried to save battery, but prevents LocationChanged events
Catch
Log(LastException.Message)
End Try
End Sub
Sub Service_Destroy
Log("loc Service_Destroy",1)
End Sub
Sub FusedLocationProvider1_LocationChanged(Location1 As Location)
Log("FusedLocationProvider1_LocationChanged")
haslocationchanged = True
Try
UpdateDistanceandNotify
Catch
Log(LastException)
End Try
Sub UpdateDistanceandNotify
Log("loc - UpdateDistanceandNotify",1)
End Sub
Sub FusedLocationProvider1_ConnectionFailed(ConnectionResult1 As Int)
Log("FusedLocationProvider1_ConnectionFailed")
' the FusedLocationProvider ConnectionResult object contains the various ConnectionResult constants
Select ConnectionResult1
Case FusedLocationProvider1.ConnectionResult.NETWORK_ERROR
' a network error has occurred, this is likely to be a recoverable error
' so try to connect again
FusedLocationProvider1.Connect
Case Else
' TODO handle other errors
End Select
End Sub
Sub FusedLocationProvider1_ConnectionSuccess
Log("FusedLocationProvider1_ConnectionSuccess")
Dim LocationRequest1 As LocationRequest
LocationRequest1.Initialize
LocationRequest1.SetInterval(1000) ' 1000 milliseconds
LocationRequest1.SetPriority(LocationRequest1.Priority.PRIORITY_HIGH_ACCURACY)
'LocationRequest1.SetPriority(LocationRequest1.Priority.PRIORITY_BALANCED_POWER_ACCURACY)
LocationRequest1.SetSmallestDisplacement(1) ' 1 meter
FusedLocationProvider1.RequestLocationUpdates(LocationRequest1)
End Sub
Sub FusedLocationProvider1_ConnectionSuspended(SuspendedCause1 As Int)
Log("FusedLocationProvider1_ConnectionSuspended")
' the FusedLocationProvider SuspendedCause object contains the various SuspendedCause constants
Select SuspendedCause1
Case FusedLocationProvider1.SuspendedCause.CAUSE_NETWORK_LOST
' TODO take action
Log("ConnectionSuspended:NETWORK_LOST")
Case FusedLocationProvider1.SuspendedCause.CAUSE_SERVICE_DISCONNECTED
' TODO take action
Log("ConnectionSuspended:SERVICE_DISCONNECTED")
End Select
End Sub
End Sub