Android Question Background service doesn't run in sleep on And 7.0

DickD

Active Member
Licensed User
I am running a service in the background which doesn't run on Android 7.0 when the phone goes to sleep mode. It runs fine on Android 6.0.1 in sleep. I thought services be definition ran in sleep but then I found the string on service.startforeground so I added it as shown in the code below. On A6.0 the service runs in sleep mode with or without service.startforegorund. On A7 it never runs in sleep mode.

B4X:
Sub Service_Create
 Server.Initialize(19257, "Server")
 Log("WIFIService Starting, MyIp = " & Server.GetMyIP)
 serial1.Initialize("Serial1")
 n1.Initialize
 n1.Icon = "icon"
 n1.AutoCancel = True
 n1.Sound = False
 n1.SetInfo("Wifi is listening","Click to stop",Stop)
 Service.StartForeground(4, n1)
End Sub

Sub Service_Start(StartingIntent As Intent)
 Server.Listen
 Log("WIFI Service is Listening")
End Sub
 

DickD

Active Member
Licensed User

On further testing it seems like the service is running but slowly. It seems to take much longer to reply to incoming requests. Is it possible that services in sleep mode run slower?
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Is it possible that services in sleep mode run slower?

As the "sleep mode" is good for energy saving, this is a normal behaviour I think (as I know Android 7.x is optimized here). Take a search for PhoneWakeState and change the behaviour (your device then does not get into deep sleep).
 
Upvote 0

DickD

Active Member
Licensed User
As the "sleep mode" is good for energy saving, this is a normal behaviour I think (as I know Android 7.x is optimized here). Take a search for PhoneWakeState and change the behaviour (your device then does not get into deep sleep).
It looks like PhoneWakeState prevents the phone from going to sleep. I don't really want to prevent that. I just want the background service module to operate at normal speed in sleep mode and respond quickly to incoming requests by Server.Listen. Is there anyway to do that in And. 7x?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Most probably no. The network connection will not be active while the device sleeps.

Maybe you need to switch to push notifications.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…