0

我想收听 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());
}
4

1 回答 1

0

注册CONNECTIVITY_ACTION广播接收器

  1. 您可以尝试两种选择:

    • 使用Context传递BroadcastReceiver的 获取ConnectivityManager 并获取有关任何活动网络的当前信息
    • 使用发送Intent
  2. 获取从第 1 点中的任何选项获得的活动网络的详细状态

公共类 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);
                    }});
            }
于 2013-07-31T15:24:38.440 回答