xamarin android,UWP 网络类型和IP地址

少年乘勇气,百战过乌孙。这篇文章主要讲述xamarin android,UWP 网络类型和IP地址相关的知识,希望能为你提供帮助。
【xamarin android,UWP 网络类型和IP地址】  App开发经常要判断网络连通情况,并判断网络类型,获取网络IP。xamarin中可以使用Dependencies提供各平台下的方法,现把各平台代码记录如下:

xamarin android,UWP 网络类型和IP地址

文章图片
xamarin android,UWP 网络类型和IP地址

文章图片
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 { 25public class NetworkStatusAndroid : INetworkStatus 26{ 27public bool HasNetwork() 28{ 29 30//Test(); 31 32ConnectivityManager cwjManager = (ConnectivityManager)Android.App.Application.Context.GetSystemService(Context.ConnectivityService); 33bool hasNetwork = true; 34if (cwjManager.ActiveNetworkInfo != null) 35{ 36hasNetwork = cwjManager.ActiveNetworkInfo.IsAvailable; 37} 38else 39{ 40hasNetwork = false; 41} 42 43return hasNetwork; 44} 45public string GetNetType() 46{ 47return getCurrentNetType(Android.App.Application.Context); 48} 49 50/// < summary> 51/// 获取网络类型 52/// < /summary> 53/// < param name="context"> < /param> 54/// < returns> < /returns> 55public static string getCurrentNetType(Context context) 56{ 57String type = ""; 58ConnectivityManager cm = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService); 59NetworkInfo info = cm.ActiveNetworkInfo; 60if (info == null) 61{ 62type = "null"; 63} 64else if (info.Type == ConnectivityType.Wifi) 65{ 66type = "wifi"; 67} 68else if (info.Type == ConnectivityType.Mobile) 69{ 70int subType = (int)info.Subtype; 71if (subType == (int)NetworkType.Cdma || subType == (int)NetworkType.Gprs 72|| subType == (int)NetworkType.Edge) 73{ 74type = "2g"; 75} 76else if (subType == (int)NetworkType.Umts || subType == (int)NetworkType.Hsdpa 77|| subType == (int)NetworkType.EvdoA || subType == (int)NetworkType.Evdo0 78|| subType == (int)NetworkType.EvdoB) 79{ 80type = "3g"; 81} 82else if (subType == (int)NetworkType.Lte) 83{// LTE是3g到4g的过渡,是3.9G的全球标准 84type = "4g"; 85} 86} 87return type; 88} 89 90/// < summary> 获取手机wifi 91/// < /summary> 92/// < returns> < /returns> 93public string GetWifiIP() 94{ 95string IP = String.Empty; 96Android.Net.Wifi.WifiManager wifi = (Android.Net.Wifi.WifiManager)Forms.Context.GetSystemService(Context.WifiService); 97if (wifi.IsWifiEnabled) 98{ 99Android.Net.Wifi.WifiInfo wifiInfo = wifi.ConnectionInfo; 100IP = Mixin.Common.StringFormat.intToIp(wifiInfo.IpAddress); 101} 102return IP; 103} 104 105/// < summary> 106/// 获取手机IP地址 107/// < /summary> 108/// < returns> < /returns> 109public string Test() 110{ 111IEnumeration ie = NetworkInterface.NetworkInterfaces; 112while (ie.HasMoreElements) 113{ 114NetworkInterface intf = ie.NextElement() as NetworkInterface; 115IEnumeration enumIpAddr = intf.InetAddresses; 116while (enumIpAddr.HasMoreElements) 117{ 118 119InetAddress inetAddress = enumIpAddr.NextElement() as InetAddress; 120 121if (!inetAddress.IsLoopbackAddress & & (inetAddress as Inet4Address) != null & & inetAddress.HostAddress.ToString() != "127.0.0.1") 122{ 123return inetAddress.HostAddress.ToString(); 124} 125} 126} 127return null; 128} 129} 130 }

Android
xamarin android,UWP 网络类型和IP地址

文章图片
xamarin android,UWP 网络类型和IP地址

文章图片
1 using Test.Dependencies; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using Windows.Networking.Connectivity; 8 using Xamarin.Forms; 9 10 [assembly: Dependency(typeof(Test.UWP.Dependencies.NetworkStatus))] 11 namespace Test.UWP.Dependencies 12 { 13public class NetworkStatus : INetworkStatus 14{ 15static string None = "None"; 16static string Unknown = "Unknown"; 17static string IIG = "2G"; 18static string IIIG = "3G"; 19static string IVG = "4G"; 20static string Wifi = "wifi"; 21static string Lan = "Lan"; 22/// < summary> 当前应用是否联网 23/// < /summary> 24/// < returns> < /returns> 25public bool HasNetwork() 26{ 27bool isConnected = false; 28 29string InternetType = null; 30ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile(); 31if (profile == null) 32{ 33InternetType = NetworkStatus.None; 34} 35else 36{ 37NetworkConnectivityLevel cl = profile.GetNetworkConnectivityLevel(); 38isConnected = (cl != NetworkConnectivityLevel.None); 39} 40return isConnected; 41} 42/// < summary> 43/// < /summary> 44/// < returns> < /returns> 45public string GetWifiIP() 46{ 47var icp = NetworkInformation.GetInternetConnectionProfile(); 48 49if (icp?.NetworkAdapter == null) return null; 50var hostname = 51NetworkInformation.GetHostNames() 52.SingleOrDefault( 53hn => 54hn.IPInformation?.NetworkAdapter != null & & hn.IPInformation.NetworkAdapter.NetworkAdapterId 55== icp.NetworkAdapter.NetworkAdapterId); 56 57// the ip address 58return hostname?.CanonicalName; 59//return null; 60} 61/// < summary> 62/// 获取UWP连接类型 63/// < /summary> 64/// < returns> < /returns> 65public string GetNetType() 66{ 67bool isConnected = false; 68 69string InternetType = null; 70ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile(); 71if (profile == null) 72{ 73InternetType = NetworkStatus.None; 74} 75else 76{ 77NetworkConnectivityLevel cl = profile.GetNetworkConnectivityLevel(); 78isConnected = (cl != NetworkConnectivityLevel.None); 79} 80if (!isConnected) 81{ 82return NetworkStatus.None; 83} 84if (profile.IsWwanConnectionProfile) 85{ 86if (profile.WwanConnectionProfileDetails == null) 87{ 88InternetType = NetworkStatus.Unknown; 89} 90WwanDataClass connectionClass = profile.WwanConnectionProfileDetails.GetCurrentDataClass(); 91switch (connectionClass) 92{ 93//2G 94case WwanDataClass.Edge: 95case WwanDataClass.Gprs: 96InternetType = NetworkStatus.IIG; 97break; 98//3G 99case WwanDataClass.Cdma1xEvdo: 100case WwanDataClass.Cdma1xEvdoRevA: 101case WwanDataClass.Cdma1xEvdoRevB: 102case WwanDataClass.Cdma1xEvdv: 103case WwanDataClass.Cdma1xRtt: 104case WwanDataClass.Cdma3xRtt: 105case WwanDataClass.CdmaUmb: 106case WwanDataClass.Umts: 107case WwanDataClass.Hsdpa: 108case WwanDataClass.Hsupa: 109InternetType = NetworkStatus.IIIG; 110break; 111//4G 112case WwanDataClass.LteAdvanced: 113InternetType = NetworkStatus.IVG; 114break; 115//无网 116case WwanDataClass.None: 117InternetType = NetworkStatus.Unknown; 118break; 119case WwanDataClass.Custom: 120default: 121InternetType = NetworkStatus.Unknown; 122break; 123} 124} 125else if (profile.IsWlanConnectionProfile) 126{ 127InternetType = NetworkStatus.Wifi; 128} 129else 130{ 131///不是Wifi也不是蜂窝数据判断为Lan 132InternetType = NetworkStatus.Lan; 133} 134return InternetType; 135} 136} 137 }

UWPios的方法还没写全后面不上,先写一部分
xamarin android,UWP 网络类型和IP地址

文章图片
xamarin android,UWP 网络类型和IP地址

文章图片
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using Foundation; 7 using UIKit; 8 using Test.iOS.Dependencies; 9 using Xamarin.Forms; 10 using Test.Dependencies; 11 using SystemConfiguration; 12 using System.Net; 13 using CoreFoundation; 14 using ObjCRuntime; 15 16 [assembly: Dependency(typeof(NetworkStatusIOS))] 17 namespace Test.iOS.Dependencies 18 { 19public class NetworkStatusIOS : INetworkStatus 20{ 21 22public bool HasNetwork() 23{ 24//创建零地址,0.0.0.0的地址表示查询本机的网络连接状态 25System.Net.IPAddress zeroAddress = System.Net.IPAddress.Parse("0.0.0.0"); 26bool hasNetWork = false; 27NetworkReachability defaultRouteReachability = new NetworkReachability(null, zeroAddress); 28NetworkReachabilityFlags flags; 29//获得连接的标志 30bool didRetrieveFlags = defaultRouteReachability.TryGetFlags(out flags); 31if (!didRetrieveFlags) 32{ 33hasNetWork = false; 34} 35//根据获得的连接标志进行判断 36bool isReachable = (flags & NetworkReachabilityFlags.Reachable) == NetworkReachabilityFlags.Reachable; 37bool needsConnection = (flags & NetworkReachabilityFlags.ConnectionRequired) == NetworkReachabilityFlags.ConnectionRequired; 38 39hasNetWork = (isReachable & & !needsConnection); 40 41return hasNetWork; 42} 43 44public string GetWifiIP() 45{ 46//SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)& zeroAddress); //创建测试连接的引用: 47System.Net.IPAddress zeroAddress = System.Net.IPAddress.Parse("0.0.0.0"); 48NetworkReachabilityFlags flags = NetworkReachabilityFlags.IsLocalAddress; 49IsAdHocWiFiNetworkAvailable(out flags); 50//NetworkReachability defaultRouteReachability = new NetworkReachability(null, zeroAddress); 51//defaultRouteReachability.TryGetFlags 52 53return ""; 54} 55public string GetNetType() 56{ 57string strNetworkType = ""; 58 59//创建零地址,0.0.0.0的地址表示查询本机的网络连接状态 60System.Net.IPAddress zeroAddress = System.Net.IPAddress.Parse("0.0.0.0"); 61bool hasNetWork = false; 62NetworkReachability defaultRouteReachability = new NetworkReachability(null, zeroAddress); 63NetworkReachabilityFlags flags; 64//获得连接的标志 65bool didRetrieveFlags = defaultRouteReachability.TryGetFlags(out flags); 66if (!didRetrieveFlags) 67{ 68hasNetWork = false; 69} 70 71if ((flags & NetworkReachabilityFlags.ConnectionRequired) == 0) 72{ 73strNetworkType = "WIFI"; 74} 75if ( 76((flags & NetworkReachabilityFlags.ConnectionOnDemand) != 0) || 77(flags & NetworkReachabilityFlags.ConnectionOnTraffic) != 0 78) 79{ 80if ((flags & NetworkReachabilityFlags.InterventionRequired) == 0) 81{ 82strNetworkType = "WIFI"; 83} 84} 85 86if ((flags & NetworkReachabilityFlags.IsWWAN)==NetworkReachabilityFlags.IsWWAN) 87{ 88if ((float)PlatformName.iOS > 7) 89{ 90CoreTelephony.CTTelephonyNetworkInfo info = new CoreTelephony.CTTelephonyNetworkInfo(); 91string currentRadioAccessTechnology = info.CurrentRadioAccessTechnology; 92if (!string.IsNullOrEmpty(currentRadioAccessTechnology)) 93{ 94if (currentRadioAccessTechnology == "CTRadioAccessTechnologyLTE") 95{ 96strNetworkType = "4G"; 97} 98else if (currentRadioAccessTechnology == "CTRadioAccessTechnologyEdge" || 99currentRadioAccessTechnology == "

    推荐阅读