App开发经常要判断网络连通情况,并判断网络类型,获取网络IP。xamarin中可以使用Dependencies提供各平台下的方法,现把各平台代码记录如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using Android.App; 7 using Android.Content; 8 using Android.OS; 9 using Android.Runtime; 10 using Android.Views; 11 using Android.Widget; 12 using Xamarin.Forms; 13 using Test.Droid.Dependencies; 14 using Android.Net; 15 using Test.Dependencies; 16 using Java.Net; 17 using Java.Util; 18 using Android.Telephony; 19 using Android.Net.Wifi; 20 using Org.Apache.Http.Conn.Util; 21 22 [assembly: Dependency(typeof(NetworkStatusAndroid))] 23 namespace Test.Droid.Dependencies 24 { 25 public class NetworkStatusAndroid : INetworkStatus 26 { 27 public bool HasNetwork() 28 { 29 30 //Test(); 31 32 ConnectivityManager cwjManager = (ConnectivityManager)Android.App.Application.Context.GetSystemService(Context.ConnectivityService); 33 bool hasNetwork = true; 34 if (cwjManager.ActiveNetworkInfo != null) 35 { 36 hasNetwork = cwjManager.ActiveNetworkInfo.IsAvailable; 37 } 38 else 39 { 40 hasNetwork = false; 41 } 42 43 return hasNetwork; 44 } 45 public string GetNetType() 46 { 47 return getCurrentNetType(Android.App.Application.Context); 48 } 49 50 /// <summary> 51 /// 获取网络类型 52 /// </summary> 53 /// <param name="context"></param> 54 /// <returns></returns> 55 public static string getCurrentNetType(Context context) 56 { 57 String type = ""; 58 ConnectivityManager cm = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService); 59 NetworkInfo info = cm.ActiveNetworkInfo; 60 if (info == null) 61 { 62 type = "null"; 63 } 64 else if (info.Type == ConnectivityType.Wifi) 65 { 66 type = "wifi"; 67 } 68 else if (info.Type == ConnectivityType.Mobile) 69 { 70 int subType = (int)info.Subtype; 71 if (subType == (int)NetworkType.Cdma || subType == (int)NetworkType.Gprs 72 || subType == (int)NetworkType.Edge) 73 { 74 type = "2g"; 75 } 76 else if (subType == (int)NetworkType.Umts || subType == (int)NetworkType.Hsdpa 77 || subType == (int)NetworkType.EvdoA || subType == (int)NetworkType.Evdo0 78 || subType == (int)NetworkType.EvdoB) 79 { 80 type = "3g"; 81 } 82 else if (subType == (int)NetworkType.Lte) 83 {// LTE是3g到4g的过渡,是3.9G的全球标准 84 type = "4g"; 85 } 86 } 87 return type; 88 } 89 90 /// <summary>获取手机wifi 91 /// </summary> 92 /// <returns></returns> 93 public string GetWifiIP() 94 { 95 string IP = String.Empty; 96 Android.Net.Wifi.WifiManager wifi = (Android.Net.Wifi.WifiManager)Forms.Context.GetSystemService(Context.WifiService); 97 if (wifi.IsWifiEnabled) 98 { 99 Android.Net.Wifi.WifiInfo wifiInfo = wifi.ConnectionInfo; 100 IP = Mixin.Common.StringFormat.intToIp(wifiInfo.IpAddress); 101 } 102 return IP; 103 } 104 105 /// <summary> 106 /// 获取手机IP地址 107 /// </summary> 108 /// <returns></returns> 109 public string Test() 110 { 111 IEnumeration ie = NetworkInterface.NetworkInterfaces; 112 while (ie.HasMoreElements) 113 { 114 NetworkInterface intf = ie.NextElement() as NetworkInterface; 115 IEnumeration enumIpAddr = intf.InetAddresses; 116 while (enumIpAddr.HasMoreElements) 117 { 118 119 InetAddress inetAddress = enumIpAddr.NextElement() as InetAddress; 120 121 if (!inetAddress.IsLoopbackAddress && (inetAddress as Inet4Address) != null && inetAddress.HostAddress.ToString() != "127.0.0.1") 122 { 123 return inetAddress.HostAddress.ToString(); 124 } 125 } 126 } 127 return null; 128 } 129 } 130 }