I am writing an app that connects to a telnet server via wifi. I have a service that manages the socket connection. It all works fine, but when the phone sleeps it disconnects the wifi radio, which causes the socket connection to break (and throws a SocketException).
I feel like I should be able to set up a a broadcast receiver whose onResume() method is called when the wifi network connection is lost, and that would allow me to gracefully shut down the socket, and re-open it if the network is immediately re-connected. But I can’t find anything like that in the doc or via searching.
Service code is here if you want it, thanks for the help, I really appreciate it!
BroadcastReceiver for ConnectivityManager.CONNECTIVITY_ACTION. In the
onReceive handler you can call
NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO) and then
info.getType() and check for
ConnectivityManager.TYPE_WIFI and do what you want then. 🙂
*set these permissions in your manifest
*Register a BroadcastReceiver for these actions filters in your manifest
*Define your BroadcastReceiver´s implementation
I know this is an old question but see the following developer documentation:
Not sure as to the exact way to do this but I think the ConnectivityManager would be a good place to start.
you can get an instance of this class by calling
There are also some other good classes in android.net that you can use.
Hope that helps.