Android Question How to eliminate StartForeground on restart

Scantech

Well-Known Member
Licensed User
Longtime User
Lets say i have GPS app that is tracking stuff. Now when user in settings does not want to track anymore, when the device restarts and startboot is true. How do i eliminate the Codes below. I just want the service to start and not do anything and not to display notification and not to startforeground?

B4X:
Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
    GPS.Initialize("gps")
    lock.PartialLock
End Sub
B4X:
Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(nid, CreateNotification("..."))
    StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
    Track
End Sub
 
Last edited:

Scantech

Well-Known Member
Licensed User
Longtime User
I was able to do something like this and seems to be working ok. Can anyone confirm these codings will not pose an issue?

B4X:
Sub Service_Create
    If blnUseGPS = True Then
        Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
    End If
    GPS.Initialize("gps")
    lock.PartialLock
End Sub
Sub Service_Start (StartingIntent As Intent)
    If blnUseGPS = true Then
        Service.StartForeground(nid, CreateNotification("..."))
    Else
        Service.StopAutomaticForeground
    End If
    StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
    Track
End Sub

With blnUseGPS = false, i had to call Service.StopAutomaticForeground or else the notification will appear. The service gets destroyed which appears normal.
 
Last edited:
Upvote 0
Top