Android Question Confusion over new Automatic Foreground Mode code

GuyBooth

Active Member
Licensed User
Longtime User
I have to say I have read the tutorials and threads concerning the Automatic Foreground Mode, and I am confused.
My first question is this. If my app calls a service to start in Foreground mode, do I have to add or change any code at all?
B4X:
Sub Service_Create
    ' Service ----------------------------------------------------------------------------------------------
    nUPnPSS_Notify.Initialize
    nUPnPSS_Notify.Icon = "icon"
    nUPnPSS_Notify.Sound=False
    nUPnPSS_Notify.Vibrate=False
    nUPnPSS_Notify.Light=False
    nUPnPSS_Notify.OnGoingEvent=True
    nUPnPSS_Notify.SetInfo("The Music Machine", "Subscriptions", "")
    nUPnPSS_Notify.Notify(2)
    Service.StartForeground(2, nUPnPSS_Notify)
    (more code)

end sub

Sub service_Start
    (code)
End sub

My second question is:
If I start a service simply by calling it from an activity, to I add the Service.StopAutomaticForeground line into the Service_Start sub like this, with nothing more required?

B4X:
' TMM_DBService
Sub Service_Create
    lstAssembled.Initialize
    lstMissingFiles.Initialize
    lstNotDownLoaded.Initialize
    lstNASMediaPaths.Initialize
    sConnection = "?" & "dbname=" & TMM.gsDBName & "&username=" & TMM.gsUserName & "&userpassword=" & TMM.gsUserPassword
    smbNas.Initialize("smbNAS")
    smbNas.SetCredentials(TMM.gsUserName,TMM.gsUserPassword,"")
End Sub

Sub Service_Start (StartingIntent As Intent)

    Service.StopAutomaticForeground
End Sub

Sub Service_Destroy
    
End Sub

Answers to these two scenarios at least will get me started ...
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. You don't need to add anything in both cases.
2. Calling Service.StopAutomaticForeground will not do any harm in both cases.

Automatic foreground mode is only entered when the service is started from a receiver while your app is in the background. Common cases: StartServiceAt, #StartAtBoot, push notifications and static intent filters.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…