当Android / GoogleTV与WAN断开连接时收听

时间:2013-07-30 02:29:19

标签: android google-tv

我想在GoogleTV设备断开连接时收听,然后显示弹出警告。

我以为我已经通过以下代码实现了这一点,但后来发现我只是在设备的以太网电缆断开时才被告知断线:即局域网。当路由器的输入以太网电缆断开时,我没有收到警报:即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());
}

1 个答案:

答案 0 :(得分:0)

为BroadcastReceiver

注册CONNECTIVITY_ACTION
  1. 您可以尝试两种方法:

    • 使用Context传递的BroadcastReceiver获取ConnectivityManager并获取有关任何有效网络的最新信息
    • 使用发送到Intent
    • 的parcerable extras
  2. 获取从第1点的任何选项中获得的有效网络的详细状态

  3. 公共类LANConnectionReceiver扩展了BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent i) {
    
                NetworkInfo info = (NetworkInfo)i.getParcelableExtra(ConnectivityManager.EXTRA_EXTRA_INFO);
                /*or use the context to get the connection manager whenever the broadcast is sent to this receiver 
                 * ConnectivityManager conmng = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
                    NetworkInfo activeNetwork = conmng.getActiveNetworkInfo();
                 */
                Log.d("ConnectionReceiver", "State=" + info.getDetailedState());
                if(info.getType() ==ConnectivityManager.TYPE_ETHERNET){ //optional, we are interested in case when it looses WAN 
                    switch(info.getDetailedState()){
                    case AUTHENTICATING:
                        break;
                    case BLOCKED:
                        break;
                    case CAPTIVE_PORTAL_CHECK:
                        break;
                    case CONNECTED:
                        break;
                    case CONNECTING:
                        break;
                    case DISCONNECTED:
                        break;
                    case DISCONNECTING:
                        break;
                    case FAILED:
                        break;
                    case IDLE:
                        break;
                    case OBTAINING_IPADDR:
                        break;
                    case SCANNING:
                        break;
                    case SUSPENDED:
                        break;
                    case VERIFYING_POOR_LINK:
                        break;
                    default:
                        break;
    
                    }
                }
        }
    
    
    
    }
    

    请注意,我们对网络的类型不感兴趣,因为我们感兴趣的是电视是否具有WAN访问权限,这就是为什么我将条件info.getType()==ConnectivityManager.TYPE_ETHERNET作为可选项。

    编辑:在上面的评论中,我提到了在不阻止应用的情况下递归检查网络。

    public void listenForConnection(final Context c, final Handler h){
                    h.postAtFrontOfQueue(new Runnable(){
    
                        @Override
                        public void run() {
                             ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
                            NetworkInfo networkInfo = cm.getActiveNetworkInfo();
                            if(networkInfo!=null && networkInfo.isConnectedOrConnecting()){
                                Intent i = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
                                i.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, networkInfo.getType());
                                i.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                                i.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, networkInfo.isFailover());
                                i.putExtra("MY_CUSTOM_KEY_HAS_WAN", true);
                                c.sendBroadcast(i);
                            }else {
                                //do something else
                            }
                            h.postDelayed(this, 100);
                        }});
                }