startforeground

wl

Well-Known Member
Licensed User
Longtime User
Hello,

I have a service that keeps a TCP connection to my server open.

I was told by Erel I should make the service running on the foregound by using "Service.StartForeground". This methods needs an Id and a Notification object. But what Notification is needed ?

Does anyone have an example ?

Or is there another way to make sure the TCP connection stays open ?

Thank you
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
You can do something like this:

B4X:
Dim n as notification   
n.Initialize
n.Icon = "icon"
n.SetInfo("Stress Reduction-Audio", "Playing Audio. Tap to open.", "Main") 
n.Sound=False
n.Vibrate=False
n.Light=False
n.OnGoingEvent=True
Service.StartForeground(1,n)

I don't recall off the top of my head, but I think you can do n.icon="" or n.icon=null to have it run in foreground mode without showing a notification.

Although you can keep the service alive this way, it's probably a better idea to periodically start the service to connect to the remote server with ServiceStartAt. Otherwise, it's rough on battery life.
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
Thanks Jack,

I understand this could cause a drain to the battery and I do understand I can use the startService at.

I'm currently launching a service that opens the TCP connection and with a timer every 10 minutes I'm checking whether the connection effectively is still open.

In the OnDestroy of the service I restart the same service again after 15 seconds.

Fact is I would like that the TCP connection remains open (since the server can push info to it at any time).

Or how is this situation best/normally handles ? Will Android decide to kill my service when it has an open TCP connection in the first place ?

Thanks,

Wim
 
Upvote 0
Top