public static bool DetectNetwork(Activity activity) { //var activity = this.Context as Activity; //public static bool DetectNetwork() //{ Android.Net.ConnectivityManager connectivityManager = (Android.Net.ConnectivityManager)activity.GetSystemService(Activity.ConnectivityService); Android.Net.NetworkInfo activeConnection = connectivityManager.ActiveNetworkInfo; //Usado apenas para analisar todos os tipos de conexao. //Android.Net.NetworkInfo[] activeConnection1 = connectivityManager.GetAllNetworkInfo();//.ActiveNetworkInfo; bool isOnline = (activeConnection != null) && activeConnection.IsConnected; if (isOnline) { // Display the type of connection //Android.Net.NetworkInfo.State activeState = activeConnection.GetState (); //_connectionType.Text = activeConnection.TypeName; // Check for a WiFi connection Android.Net.NetworkInfo wifiInfo = connectivityManager.GetNetworkInfo(Android.Net.ConnectivityType.Wifi); if (wifiInfo.IsConnected) { //Wifi connected return(true); } else { //Wifi disconnected // Check if roaming or mobile Android.Net.NetworkInfo mobileInfo = connectivityManager.GetNetworkInfo(Android.Net.ConnectivityType.Mobile); if (mobileInfo.IsRoaming || mobileInfo.IsConnected) { //roaming or mobile connected. return(true); } else { //roaming or mobile disconnected return(false); } } } else { return(false); } }
public static void ExecuteNetworkCommand(ExecuteNetworkDelegate command, object connManager) { #if Android if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.Lollipop) { command(); return; } Android.Net.Network bindNetwork = null; Android.Net.ConnectivityManager connectivityManager = connManager as Android.Net.ConnectivityManager; // ReSharper disable once UseNullPropagation if (connectivityManager != null) { Android.Net.Network[] networks = connectivityManager.GetAllNetworks(); if (networks != null) { foreach (Android.Net.Network network in networks) { Android.Net.NetworkInfo networkInfo = connectivityManager.GetNetworkInfo(network); Android.Net.NetworkCapabilities networkCapabilities = connectivityManager.GetNetworkCapabilities(network); // HasTransport support started also with Lollipop if (networkInfo != null && networkInfo.IsConnected && networkCapabilities != null && networkCapabilities.HasTransport(Android.Net.TransportType.Wifi)) { bindNetwork = network; break; } } } } if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.M) { #pragma warning disable 618 Android.Net.Network defaultNetwork = Android.Net.ConnectivityManager.ProcessDefaultNetwork; try { Android.Net.ConnectivityManager.SetProcessDefaultNetwork(bindNetwork); command(); } finally { Android.Net.ConnectivityManager.SetProcessDefaultNetwork(defaultNetwork); } #pragma warning restore 618 return; } Android.Net.Network boundNetwork = connectivityManager?.BoundNetworkForProcess; try { connectivityManager?.BindProcessToNetwork(bindNetwork); command(); } finally { connectivityManager?.BindProcessToNetwork(boundNetwork); } #else command(); #endif }
public static bool IsNetworkAvailable(Android.Net.ConnectivityManager connectivityManager) { if (connectivityManager == null) { throw new ArgumentNullException(nameof(connectivityManager)); } //When on API 21+ need to use getAllNetworks, else fall base to GetAllNetworkInfo //https://developer.android.com/reference/android/net/ConnectivityManager.html#getAllNetworks() if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop) { foreach (var network in connectivityManager.GetAllNetworks()) { var info = connectivityManager.GetNetworkInfo(network); if (info?.IsConnected ?? false) { return(true); } } } else { var info = connectivityManager.ActiveNetworkInfo; return(info?.IsConnected ?? false); } return(false); }
public static void ExecuteNetworkCommand(ExecuteNetworkDelegate command, object connManager, bool checkEthernet = false) { #if Android if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.Lollipop) { command(); return; } Android.Net.Network bindNetwork = null; Android.Net.ConnectivityManager connectivityManager = connManager as Android.Net.ConnectivityManager; // ReSharper disable once UseNullPropagation if (connectivityManager != null) { Android.Net.Network[] networks = connectivityManager.GetAllNetworks(); if (networks != null) { foreach (Android.Net.Network network in networks) { Android.Net.NetworkInfo networkInfo = connectivityManager.GetNetworkInfo(network); Android.Net.NetworkCapabilities networkCapabilities = connectivityManager.GetNetworkCapabilities(network); // HasTransport support started also with Lollipop if (networkInfo != null && networkInfo.IsConnected && networkCapabilities != null) { bool linkValid = false; bool autoIp = false; Android.Net.LinkProperties linkProperties = connectivityManager.GetLinkProperties(network); foreach (Android.Net.LinkAddress linkAddress in linkProperties.LinkAddresses) { if (linkAddress.Address is Java.Net.Inet4Address inet4Address) { if (inet4Address.IsSiteLocalAddress || inet4Address.IsLinkLocalAddress) { linkValid = true; autoIp = inet4Address.IsLinkLocalAddress; break; } } } if (linkValid) { if (networkCapabilities.HasTransport(Android.Net.TransportType.Wifi)) { bindNetwork = network; } if (checkEthernet && networkCapabilities.HasTransport(Android.Net.TransportType.Ethernet)) { if (autoIp) { // prefer Ethernet auto ip bindNetwork = network; break; } if (bindNetwork == null) { // prefer Wifi bindNetwork = network; } } } } } } } if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.M) { #pragma warning disable 618 Android.Net.Network defaultNetwork = Android.Net.ConnectivityManager.ProcessDefaultNetwork; try { Android.Net.ConnectivityManager.SetProcessDefaultNetwork(bindNetwork); command(); } finally { Android.Net.ConnectivityManager.SetProcessDefaultNetwork(defaultNetwork); } #pragma warning restore 618 return; } Android.Net.Network boundNetwork = connectivityManager?.BoundNetworkForProcess; try { connectivityManager?.BindProcessToNetwork(bindNetwork); command(); } finally { connectivityManager?.BindProcessToNetwork(boundNetwork); } #else command(); #endif }