我想收听 GoogleTV 设备何时断开连接,然后显示弹出警告。
我以为我已经通过下面的代码实现了这一点,但后来发现我只是在设备的以太网电缆断开时才被警告断开连接:即 LAN。当路由器的输入以太网电缆断开连接时,我不会收到警报:即 WAN。
我发现当断开 LAN 电缆时,GTV 设备将丢失其 IP 地址,但在断开 WAN 电缆时,GTV 设备仍将有一个 IP 地址 - 这就是为什么我没有收到应用程序丢失连接的警报.
那么如何检查 GoogleTV 设备何时不再连接到 WAN 呢?我需要在下面的代码中添加什么来做到这一点?
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());
}