I have extended the Application class in my app. In that class, I have a BroadcastReceiver that waits for Connectivity changes. If I switch to airplane mode, it instantly recognizes a connectivity lost and the broadcast is received, however if I lose my data connectivity (ie being underground, etc), it takes a few seconds to a few minutes to fire even though no internet related tasks are successfully completing (even if you try browsing in the browser). Is there a more precise way to find out when internet connectivity is first lost?
A way to find out when Internet connectivity is lost is to do proper exception checking for network errors.
If a networking exception is thrown (socket timeout, I/O error, etc) you can set the state of the application to disconnected. Of course this only works when you are actively trying to send or receive data over the network.
If connectivity notification is that important you can do periodical checks by using a remote server (ping a google server for example), but this will drain your battery as touching the network too often tends to keep the radio awake and draw power.
Related
I need to build an app that can upload data when the wifi connection matches (based on BSSID or SSID) the one defined by user or the wifi connection changes (anyway, the app needs to keep checking wifi connection or react based on wifi connection changing), and the app has to run this service in the background even the app is killed for a very long time. As far as I know, there're 2 approaches: 1. set up a broadcaster upon the changing of wifi connection and trigger the event, but it seems that broadcast for triggering certain task is not reliable in a long time; 2. set up a workManager to schedule a periodic detection of wifi connection and upload data if needed for every several mins, like 15min. Which one is the best? Or are there any other better choices?
I have a permanently running service app on the handset, one of the things it does is detect when there is an incoming phone call and send a message and some data to a companion app on a wearable device.
I'm wondering whether the app should establish the API to communicate with the wearable when it launches, or only when there is an incoming call and then disconnect afterwards.
Has anybody with wear development experience got any pros or cons of these approaches?
The service automatically starts at device start-up, and I've noticed if an attempt is made to create the GoogleApiClient/Wearable.API and get the wearable device node soon after rebooting there's a high chance of failure, therefore a disadvantage of establishing the wearable connection at app launch is its probably fail and will need to re-try or wait etc.
In general, to save on battery life, you want to minimize usage of any network connection on a mobile device. The general rule of thumb is: establish a connection only when needed, if you expect to use it again "soon" (e.g. within a minute or so), then keep it around, and close the connection when you are not going to use it for a longer while.
So in your case, since you are responding to phone calls (which should not happen every couple of minutes!), you should re-establish the connection every time. I am not sure though about the delay incurred in this case.
I am trying to build an application/service that will execute some coding everytime I connect to a specific wifi network (to log into that network and then keep the connection active ). I do already have the code for the login part and it is working fine, but I don't really know how I can automatically run this code when I connect to a network.
Ideally, I would like to build a service that starts when I join a specific wifi network, keeps the connection active (like makes a request every 10 min to keep the connection active) and finally stops when I leave that network.
I know how to build the service and keep it active, but don't know how to start and stop it automatically when joining / leaving a wifi network. Any suggestions on how to do this is appreciated. If my approach is not correct and there is a better/simpler way to do that, please let me know
Regards,
Marcel
you need a broadcast receiver.
this broadcast receiver will receives broadcasts of Wifi connected and wifi disconnected actions and from there you can start and stop your service.
Implement a BroadcastReceiver listening for the scan results from WifiManager.startScan()
Have a look at Android Scan for Wifi networks
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
I've encountered a problem with mobile internet connection after phone wake up from sleep mode. There is an application which updates some data periodically. AlarmManager triggers my BroadcastReceiver which starts service. service acquires PARTIAL_WAKE_LOCK and some http requests are sent. Unfortunately when update interval is quite big (5h) service could not send/download anything. Problem doesn't appear when I'm using WIFI connection instead of mobile and WIFI sleep policy is set to never. Everything is OK on mobile connection when update interval is short (less than half an hour). I'm also sure 5h event is received by my BroadcastReceiver because I've checked it.
How such events should be handled. I've tried to check whether connection is established by ConnectivityManager and retry 5 times if not but still same problem appears.
Thanks for any answer in advance
The Android system turns of WiFi a few minutes when the device is suspended (i.e. screen turned off).
When the device turns on again (by acquiring the wake-lock), it will take a few seconds until network connection is re-established (this is also true for 3G data connections, which are also torn-down while device is sleeping).
My suggestion:
- Wait for ConnectivityManager's broadcast that the network is up again. Practice proves that waiting 2-3 seconds after the broadcast is received helps things to settle before starting to send and receive data.
- A bit easier: After grabbing the wake-lock, wait a longer timeout and then try to connect.