Android Question [SOLVED] - GNSS - Keeping the GPS on when exiting my app


Well-Known Member
Licensed User

I'm using the GNSS library and would like to know how to prevent it from turning off the GPS when exiting my app or pressing the phone home button. We need to make sure the GPS stays on even if the app is not showing on the screen. All of the GPS coding is in the Starter module because Starter does not get killed by Android.

I do have coding that stops the GPS only in the Starter module but there isn't any coding yet that calls that sub routine.

By the way, if I use the GPS library instead of the GNSS library the GPS also turns off when I exit the app. I'd rather use GNSS instead though. I think I will also try a 1 second timer and keep turning on the GPS to see what happens.


Here's the coding in my Starter service.

Starter Module:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#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 kvs As KeyValueStore
    Dim rp As RuntimePermissions
    Dim GPS1 As GNSS
    Private gpsStarted As Boolean
    Private strShared As String
End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.

End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub

' GPS sub routines.
Sub StartGps
    If IsPaused(Main) = False Then
        ' It means the user is on the main screen so display "Please Wait".
        CallSub(Main, "PleaseWait")
    End If

    If gpsStarted = False Then
        GPS1.Start(0, 0)
        gpsStarted = True
    End If
End Sub

Sub StopGps
    Log("at stopgps")
    If IsPaused(Main) = False Then
        ' It means the user is on the main screen so remove "Please Wait" message.
        CallSub(Main, "PleaseWaitOff")
    End If

    If gpsStarted Then
        gpsStarted = False
    End If
End Sub

Sub GPS_LocationChanged (Location1 As Location)

    CallSub(Main, "PleaseWaitOff")

End Sub

' Additional sub routines.
Sub InitializeObjects
End Sub

Sub SetupTheSettingsDatabase
    strShared = rp.GetSafeDirDefaultExternal("Settings")
    kvs.Initialize(strShared, "Settings")
'    kvs.DeleteAll

    If kvs.ContainsKey("AutoResponderMode") = False Then
        kvs.Put("AutoResponderMode", "Active")
    End If

    ' Flags.
    If kvs.ContainsKey("BlockTexts") = False Then
        kvs.Put("BlockTexts", False)
    End If

    If kvs.ContainsKey("BlockAllTexts") = False Then
        kvs.Put("BlockAllTexts", False)
    End If
    If kvs.ContainsKey("BlockAllTextsWhenVehicleMoves") = False Then
        kvs.Put("BlockAllTextsWhenVehicleMoves", True)
    End If

    If kvs.ContainsKey("BlockCalls") = False Then
        kvs.Put("BlockCalls", False)
    End If

    If kvs.ContainsKey("BlockAllCalls") = False Then
        kvs.Put("BlockAllCalls", False)
    End If

    If kvs.ContainsKey("BlockAllCallsWhenVehicleMoves") = False Then
        kvs.Put("BlockAllCallsWhenVehicleMoves", True)
    End If
End Sub
Last edited: