mono 判断系统的网络是否可用

时间:2024-01-18 22:33:44

/**
  * 判断系统的网络是否可用
  * @return
  */
        private bool isNetworkConnected()
        {
            bool net = false;

ConnectivityManager connManager = (ConnectivityManager)this.GetSystemService(ConnectivityService);

// connManager.ActiveNetworkInfo ==null   手机无法连接网络
           if (connManager.ActiveNetworkInfo != null)
           {
               //获得 wifi 连接管理
               NetworkInfo networkInfo = connManager.GetNetworkInfo(ConnectivityType.Wifi);
               //获得 GPRS 连接管理
               NetworkInfo gp = connManager.GetNetworkInfo(ConnectivityType.Mobile);
               if (networkInfo != null && networkInfo.IsAvailable != false )
               {
                   Toast.MakeText(this, "WIFI打开!", ToastLength.Long).Show();
                   net = true;
               }
              // gp.IsConnected==false 有信号无网络
               if (gp!=null && gp.IsConnected != false)
               {
                   Toast.MakeText(this, "有信号gprs打开!", ToastLength.Long).Show();
                   net = true;
               }

}
           return net;

}