How can I monitor the network connection status in Android?
I have a Service that is downloading a file from a server. I warn the user to only download the file over Wireless LAN before the download starts.
My problem is that my download gets stuck if I loose the network connection. Is there a way to listen for changes in network connection or if it is lost entirely?
Listen for CONNECTIVITY_ACTION
This looks like good sample code. Here is a snippet:
BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.w("Network Listener", "Network Type Changed");
}
};
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filter);