I am working on a similar application that starts a service that establishes a permanent TCP connection to the server at startup in order to be able to receive messages from the server. The service does not receive locks. This part of the application was implemented some time ago, and so far I have not had any problems with the fact that you could not receive messages from the server. After reading your question, I decided to check why I did not have this problem.
I thought that even if I do not hold any locks, perhaps there are other applications that therefore keep the processor running. Running adb shell dumpsys
, I noticed that this is not the case: mLocks.size=0
.
This should mean that the application can still receive packets, even if the device is sleeping. I could not find anything official here, but a few posts on the Internet seem to agree:
Although I have not found a good example of where this is documented, it seems that even if your phone is asleep, if the data is received in the connection, your code will be woken up [...] ( source )
> Say that the device is in deep sleep and the network stack is receiving an incoming packet. Will it wake up the device?
Must. ( source )
However, note that both sources recommend that you acquire a tracking lock for packet processing to prevent the device from falling asleep during this processing. I do not do this in my application (maybe I should), but my processing is really short.
When you say that your requests do not reach your application, are you sure that this is not so? Maybe they are, but your application falls asleep before it can send a response? Try to get a lock after you receive data on your socket and release it after processing is complete.
source share