How long is possible to keep connection by websocket ?
I mean is if I got the app working in background how will look the consequences
for internet download and battery life?
Socket will be closed when close all your streams or server shuts down your connection,other wise its turns off when your app turns off.
Related
I have a web app that is now being used in an Android app by launching it in a webview. Most of the Android functionality is native & the webview part is mainly just for communication via socket.io
It all works okay but as soon as the phone goes to sleep it kills the socket connection after a few minutes. This connection is used for incoming messaging so I need the connection to stay alive.
I tried creating a foreground service and using it as a keep-alive but since the socket connection is in the webview, it still gets closed no matter what I do i.e. the service stays alive and I can make a new network connection, but I can't keep the existing app connection alive.
Is there a way to keep a webview socket connection alive when the phone dozes? Am I doing something wrong? Or is my design flawed and I need a different approach?
I am creating an Android application with Socket IO
But I checked from log, the socket is not disconnected when app go to background, I don't need to use socket when app is in background. So, is it correct? Does it drain battery much when socket still works in background?
If I want to force disconnect socket in background, how can I do it?
Thanks for reading this
Situation:
I'm developing an Android app using a socket for communication with a server via the websocket protocol.
The starting point to open the socket is a Service started from the application context. Opening and communicating with the server works fine.
Problem / Question:
The socket is not closed, when the device is going in mode SCREEN_OFF.
So my question / concern is, what is really going on with my process when the device is in such power state? Should i react and close the socket to save battery? The websocket is also using ping/pong mechanism to keep the connection alive. So if the system never "stops" my process i will always communicate until the battery is down or the OS kills my app?
Or am i understanding something wrong?
When device go in sleep mode, you have to close all service, thread to save battery.
I want to develop an application that use the internet connection and the bluetooth connection in background. Each 5min my application get information from my bluetooth device and send the data on a server via the internet connection. It's possible to have internet and bluetooth connection in background on android?
Yes this would be possible, You would want to use some form of service or ASyncTask to handle this which could sleep for five minutes to avoid any excess battery drain. You will also need to ensure that the app does not close when the user leaves the app so that you can continue sending and receiving data.
Here's some help on the internet side of things:
http://developer.android.com/training/basics/network-ops/connecting.html
and bluetooth:
http://developer.android.com/reference/android/bluetooth/package-summary.html
That should be enough for you to get started, best of luck! :)
i'm writing a client-server application which uses TCP socket connection. In my android project, Service creates a thread for listening the TCP socket.
Everything seems OK. But i have got one problem.. My network service running in background fine; But some time later (10-15 min..), when i try to open my application (main activity) again, I can't get responses from the socket connection. It freezes or something?? i cant send or get TCP messages from the socket.. What can be the reason of this? I'm working on my phone, via 3G connection.
(Besides, the app running in the emulator hasn't got such this problem; I assume Its connection is stable, long-during )
Thank you for your answering.
Due to power optimizations and perhaps changes in connectivity (GPRS/HSDPA/Wifi) it's very likely your connection is being dropped.
In order to maintain a connection, your background service needs to claim a wakelock using the PowerManager class. This prevents the device from going to power-saving mode and thus disconnecting your socket. But beware, this significantly lowers the battery life of the device.
Also, you need to handle changes in connectivity which break your open connection. Android sends out a broadcast message named android.net.conn.CONNECTIVITY_CHANGE to notify of changes in connectivity.
Depending on your use-case I would poll with when the device is in the sleep-mode and only build a connection when the device is actively in use or just use C2DM push notifications
When I have experienced something like this in my apps, it has usually been because of power optimisations on the phone (which cant be overridden). If the socket has been idle for too long, it is automatically closed and needs to be reopened.
Are you sending data from time to time? Like implementing a heartbeat protocol ? if you are not, you should...or maybe it has to do with socket READ/WRITE TIMEOUT