question

Gaurav avatar image
Gaurav asked ·

TCP Socket in BG service is terminated when device enters sleep mode.

Hi All

I have created a background service in which I am using the tcp socket connection. When the app is live either in background/foreground, the connection is working as expected. But when the clover device goes to sleep, the service is stopped terminating the socket. I have used the WakeLock with PowerManager.PARTIAL_WAKE_LOCK and WifiLock to keep the CPU alive, but this also does not keep the cpu alive. It kills my service again.

Then I used the PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE flags so that my clover device be in DIM state and it works. This is not a correct approach to fix this as this always keep the clover screen ON.

Please let us know the suggested solution for that.
Clover MiniClover Android SDKClover Station
1 comment
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Guaravdeep,

Any specific reason why your app should retain the TCP connection open?
0 Likes 0 · ·

1 Answer

Raymond Lee avatar image
Raymond Lee answered ·
Hi @Gaurav,

Just an FYI, This sounds like an Android question that is non Clover specific, you might get more responses from posting on a different forum.

That being said, it sounds like you are trying to create an never ending background service. The only way to do this is by creating a new service every time your old service is destroyed. Your service will have to re-establish the TCP socket connection when it is started again.

This blog post goes into detail on creating the never ending background service:
https://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android

Hope that helps,
Raymond
Share
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Welcome to the
Clover Developer Community