Using FusedLocation in B4i, I detect when the device is stationary or the battery is low with MotionActivity, and then apply this low power mode:
When MotionActivity detects movement again, I disable low power mode and set the normal or intensive tracking mode:
B4X:
Public Sub ApplyLowPowerTracking(lm As LocationManager)
If lm = Null Then Return
currentSignificantMode = True
Dim no As NativeObject = lm
no = no.GetField("manager")
If no.IsInitialized = False Then Return
If App.OSVersion >= 9 Then no.RunMethod("setAllowsBackgroundLocationUpdates:", Array(True))
no.SetField("desiredAccuracy", 1000)
no.SetField("distanceFilter", 1000)
no.SetField("pausesLocationUpdatesAutomatically", True)
no.SetField("activityType", 1) ' CLActivityTypeFitness
EnableSCM(lm) ' startMonitoringSignificantLocationChanges
End Sub
When MotionActivity detects movement again, I disable low power mode and set the normal or intensive tracking mode:
B4X:
Public Sub SetTrackingMode(Intensive As Boolean)
currentTrackingIntensive = Intensive
If fl.IsInitialized = False Or fl.FLP = Null Then Return
Dim no As NativeObject = fl.FLP
no = no.GetField("manager")
Try
no.SetField("desiredAccuracy", IIf(Intensive, 50, 100))
no.SetField("distanceFilter", IIf(Intensive, 1, 10))
no.SetField("pausesLocationUpdatesAutomatically", False)
Catch
Log("Error configuring TrackingMode: " & LastException)
Return
End Try
If IsValidLocationManager(fl.FLP) = False Then
gpsWatchdog_Tick
End If
End Sub