I want to listen for when the GoogleTV device is disconnected and then display a pop up warning.
I thought I had achieved this by the below code, but then discovered that I am only warned about the disconnection when the device's ethernet cable is disconnected: i.e. LAN. I am not alerted when the router's input ethernet cable is disconnected: i.e. WAN.
I have discovered that when disconnecting the LAN cable, the GTV device will lose its IP address, but when disconnecting the WAN cable, the GTV device WILL still have an IP address - that is why it I am not alerted that the app lost a connection.
So how do I check when a GoogleTV device is no longer connected to a WAN? What do I need to add to the below code to do that?
startListeningToNetwork();
private void startListeningToNetwork() {
if(_networkStateReceiver == null){
//Listen for when the network changes. If app loses internet before webView has loaded, then display error message.
_networkStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(!isConnected()){
showNetworkErrorDialog();
} else {
closeNetworkError();
}
}
};
IntentFilter networkFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
getActivity().registerReceiver(_networkStateReceiver, networkFilter);
}
}
private boolean isConnected() {
ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
Toast.makeText(getActivity(), "isConnected? " + (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) , Toast.LENGTH_SHORT).show();
return (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected());
}