Exemple #1
0
        /// <summary>
        /// 检查网络是否可用
        /// </summary>
        /// <returns><c>true</c>, if network available was checked, <c>false</c> otherwise.</returns>
        /// <param name="activity">Activity.</param>
        public static bool CheckNetworkAvailable(Activity activity)
        {
            Context context = activity.ApplicationContext;
            // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
            ConnectivityManager connectivityManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);

            if (connectivityManager == null)
            {
                return(false);
            }
            else
            {
                // 获取NetworkInfo对象
                NetworkInfo[] networkInfo = connectivityManager.GetAllNetworkInfo();
                if (networkInfo != null && networkInfo.Length > 0)
                {
                    for (int i = 0; i < networkInfo.Length; i++)
                    {
                        // 判断当前网络状态是否为连接状态
                        if (networkInfo[i].GetState() == NetworkInfo.State.Connected)
                        {
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Exemple #2
0
        public static bool CheckInternetConnection(Context ctx)
        {
            if (ctx == null)
            {
                return(true);
            }
            ConnectivityManager cm = (ConnectivityManager)ctx.GetSystemService(Context.ConnectivityService);

            NetworkInfo[] networkInfo     = cm.GetAllNetworkInfo();
            bool          connectedWifi   = false;
            bool          connectedMobile = false;

            foreach (NetworkInfo ni in networkInfo)
            {
                if (ni.TypeName.ToLower().Equals("wifi"))
                {
                    connectedWifi = ni.IsConnected;
                }
                if (ni.TypeName.ToLower().Equals("mobile"))
                {
                    connectedMobile = ni.IsConnected;
                }
            }

            bool result = connectedWifi || connectedMobile;

            //  if (!result)
            //    Toast.MakeText(ctx, "Please check your internet connection", ToastLength.Short).Show();
            return(result);
        }
Exemple #3
0
        public static IEnumerable <ConnectionType> GetConnectionTypes(ConnectivityManager manager)
        {
            //When on API 21+ need to use getAllNetworks, else fall base to GetAllNetworkInfo
            //https://developer.android.com/reference/android/net/ConnectivityManager.html#getAllNetworks()
            if ((int)Android.OS.Build.VERSION.SdkInt >= 21)
            {
                foreach (var network in manager.GetAllNetworks())
                {
                    var info = manager.GetNetworkInfo(network);

                    if (info?.Type == null)
                    {
                        yield return(ConnectionType.Other);
                    }


                    yield return(GetConnectionType(info.Type));
                }
            }
            else
            {
                foreach (var info in manager.GetAllNetworkInfo())
                {
                    if (info?.Type == null)
                    {
                        yield return(ConnectionType.Other);
                    }

                    yield return(GetConnectionType(info.Type));
                }
            }
        }
 private bool CheckConnectivity()
 {
     foreach (var item in connMan.GetAllNetworkInfo())
     {
         if ((item.Type == ConnectivityType.Mobile || item.Type == ConnectivityType.Wifi) && item.IsConnected)
         {
             return(true);
         }
     }
     return(false);
 }
Exemple #5
0
        public static bool GetIsConnected(ConnectivityManager manager)
        {
            try
            {
                //When on API 21+ need to use getAllNetworks, else fall base to GetAllNetworkInfo
                //https://developer.android.com/reference/android/net/ConnectivityManager.html#getAllNetworks()
                if ((int)Android.OS.Build.VERSION.SdkInt >= 21)
                {
                    foreach (var network in manager.GetAllNetworks())
                    {
                        try
                        {
                            var info = manager.GetNetworkInfo(network);

                            if (info == null || !info.IsAvailable)
                            {
                                continue;
                            }

                            if (info.IsConnected)
                            {
                                return(true);
                            }
                        }
                        catch
                        {
                            //there is a possibility, but don't worry
                        }
                    }
                }
                else
                {
                    foreach (var info in manager.GetAllNetworkInfo())
                    {
                        if (info == null || !info.IsAvailable)
                        {
                            continue;
                        }

                        if (info.IsConnected)
                        {
                            return(true);
                        }
                    }
                }

                return(false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("Unable to get connected state - do you have ACCESS_NETWORK_STATE permission? - error: {0}", e);
                return(false);
            }
        }
        internal IEnumerable <ConnectionType> GetConnectionTypes(ConnectivityManager manager)
        {
            //When on API 21+ need to use getAllNetworks, else fall base to GetAllNetworkInfo
            //https://developer.android.com/reference/android/net/ConnectivityManager.html#getAllNetworks()
            if ((int)global::Android.OS.Build.VERSION.SdkInt >= 21)
            {
                foreach (var network in manager.GetAllNetworks())
                {
                    NetworkInfo info = null;
                    try
                    {
                        info = manager.GetNetworkInfo(network);
                    }
                    catch
                    {
                        //there is a possibility, but don't worry about it
                    }

                    if (info == null)
                    {
                        continue;
                    }

                    if (!info.IsAvailable)
                    {
                        yield return(ConnectionType.None);
                    }

                    yield return(GetConnectionType(info.Type));
                }
            }
            else
            {
#pragma warning disable CS0618 // Type or member is obsolete
                foreach (var info in manager.GetAllNetworkInfo())
#pragma warning restore CS0618 // Type or member is obsolete
                {
                    if (info == null)
                    {
                        continue;
                    }

                    if (!info.IsAvailable)
                    {
                        yield return(ConnectionType.None);
                    }

                    yield return(GetConnectionType(info.Type));
                }
            }
        }
Exemple #7
0
        public void Connect(string leonetId, string leonetPassword)
        {

            NetworkInfo activeConnection = _connectivityManager.ActiveNetworkInfo;

            bool isOnline = (activeConnection != null) && activeConnection.IsConnected;

            if (isOnline)
            {
                // Networkのタイプを表示
                NetworkInfo.State activeState = activeConnection.GetState();
                System.Diagnostics.Debug.WriteLine(activeConnection.TypeName);

                // 全接続を取得して、それぞれの接続状態を確認
                // GetAllNetworks()は5.0以上でそれ以前はgetAllNetworkInfo()を使用
                if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
                {
                    Network[] allNetworks = _connectivityManager.GetAllNetworks();
                    foreach (var network in allNetworks)
                    {
                        
                        NetworkInfo info = _connectivityManager.GetNetworkInfo(network);
                        var connect = info.IsConnectedOrConnecting ? "cennected" : "disconnected";
                        System.Diagnostics.Debug.WriteLine($"{info.TypeName} is {connect}");

                        if (info.Type == ConnectivityType.Wifi)
                        {
                            Authenticator.SetDefault(new BasicAuthenticator(leonetId, leonetPassword));
                            var leonetUrlString = "http://#GATEWAY#/login.cgi".Replace("#GATEWAY#", GetDefaultGateway());
                            var leonetUrl = new URL(leonetUrlString);
                            Task.Run(() =>
                            {
                                network.OpenConnection(leonetUrl);
                            });
                            
                        }

                    }
                }
                else
                {
                    NetworkInfo[] allNetworks = _connectivityManager.GetAllNetworkInfo();
                    foreach (var item in allNetworks)
                    {
                        var connect = item.IsConnectedOrConnecting ? "cennected" : "disconnected";
                        System.Diagnostics.Debug.WriteLine($"{item.TypeName} is {connect}");
                    }
                }
            }
        }
Exemple #8
0
        public static bool IsNetworkConnected(Context context)
        {
            // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理).CONNECTIVITY_SERVICE
            ConnectivityManager connectivityManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);

            NetworkInfo[] networkInfo = connectivityManager.GetAllNetworkInfo();

            foreach (NetworkInfo item in networkInfo)
            {
                if (item.GetState() == NetworkInfo.State.Connected)
                {
                    return(true);
                }
            }
            return(false);
        }
        public static Boolean isConnectingToInternet(Context context)
        {
            ConnectivityManager connectivity = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);

            if (connectivity != null)
            {
                NetworkInfo[] info = connectivity.GetAllNetworkInfo();
                if (info != null)
                {
                    for (int i = 0; i < info.Length; i++)
                    {
                        if (info[i].GetState() == NetworkInfo.State.Connected)
                        {
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Exemple #10
0
        public static bool IsConnectedToInternet(Context context)
        {
            ConnectivityManager manager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);

            if (manager != null)
            {
                NetworkInfo[] info = manager.GetAllNetworkInfo();
                if (info != null)
                {
                    foreach (var item in info)
                    {
                        if (item.GetState() == NetworkInfo.State.Connected)
                        {
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Exemple #11
0
        public static IEnumerable <ConnectionType> GetConnectionTypes(ConnectivityManager manager)
        {
            //When on API 21+ need to use getAllNetworks, else fall base to GetAllNetworkInfo
            //https://developer.android.com/reference/android/net/ConnectivityManager.html#getAllNetworks()
            if ((int)Android.OS.Build.VERSION.SdkInt >= 21)
            {
                foreach (var network in manager.GetAllNetworks())
                {
                    NetworkInfo info = null;
                    try
                    {
                        info = manager.GetNetworkInfo(network);
                    }
                    catch
                    {
                        //there is a possibility, but don't worry about it
                    }

                    if (info == null || !info.IsAvailable)
                    {
                        continue;
                    }

                    yield return(GetConnectionType(info.Type, info.TypeName));
                }
            }
            else
            {
                foreach (var info in manager.GetAllNetworkInfo())
                {
                    if (info == null || !info.IsAvailable)
                    {
                        continue;
                    }

                    yield return(GetConnectionType(info.Type, info.TypeName));
                }
            }
        }
Exemple #12
0
        // Check Connectivity
        public static bool isNetworkAvailable(Context mContext)
        {
            try
            {
                ConnectivityManager cm = (ConnectivityManager)mContext.GetSystemService(Context.ConnectivityService);
                foreach (var info in cm.GetAllNetworkInfo())
                {
                    if (info == null)
                    {
                        continue;
                    }

                    if (info.IsConnected)
                    {
                        return(true);
                    }
                }
            }
            catch (Exception e)
            {
            }
            return(false);
        }
Exemple #13
0
        public static bool isNetworkAvailable(Context context)
        {
            ConnectivityManager cm = (ConnectivityManager)context
                                     .GetSystemService(Context.ConnectivityService);

            if (cm == null)
            {
            }
            else
            {
                NetworkInfo[] info = cm.GetAllNetworkInfo();
                if (info != null)
                {
                    for (int i = 0; i < info.Length; i++)
                    {
                        if (info [i].GetState() == NetworkInfo.State.Connected)
                        {
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Exemple #14
0
        public static bool verificaConeccion(Context context)
        {
            ConnectivityManager connectivity = ((ConnectivityManager)context.GetSystemService(Context.ConnectivityService));

            if (connectivity != null)
            {
                if (connectivity != null)
                {
                    NetworkInfo[] info = connectivity.GetAllNetworkInfo();

                    if (info != null)
                    {
                        for (int i = 0; i < info.Length; i++)
                        {
                            if (info[i].GetState() == NetworkInfo.State.Connected)
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }
Exemple #15
0
        public static bool GetIsConnected(ConnectivityManager manager)
        {
            try
            {
                //When on API 21+ need to use getAllNetworks, else fall base to GetAllNetworkInfo
                //https://developer.android.com/reference/android/net/ConnectivityManager.html#getAllNetworks()
                if ((int)Android.OS.Build.VERSION.SdkInt >= 21)
                {
                    foreach (var network in manager.GetAllNetworks())
                    {
                        try
                        {
                            var capabilities = manager.GetNetworkCapabilities(network);

                            if (capabilities == null)
                            {
                                continue;
                            }

                            //check to see if it has the internet capability
                            if (!capabilities.HasCapability(NetCapability.Internet))
                            {
                                continue;
                            }

                            //if on 23+ then we can also check validated
                            //Indicates that connectivity on this network was successfully validated.
                            //this means that you can be connected to wifi and has internet
                            if ((int)Android.OS.Build.VERSION.SdkInt >= 23 && !capabilities.HasCapability(NetCapability.Validated))
                            {
                                continue;
                            }

                            var info = manager.GetNetworkInfo(network);

                            if (info == null || !info.IsAvailable)
                            {
                                continue;
                            }

                            if (info.IsConnected)
                            {
                                return(true);
                            }
                        }
                        catch
                        {
                            //there is a possibility, but don't worry
                        }
                    }
                }
                else
                {
#pragma warning disable CS0618 // Type or member is obsolete
                    foreach (var info in manager.GetAllNetworkInfo())
#pragma warning restore CS0618 // Type or member is obsolete
                    {
                        if (info == null || !info.IsAvailable)
                        {
                            continue;
                        }

                        if (info.IsConnected)
                        {
                            return(true);
                        }
                    }
                }

                return(false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("Unable to get connected state - do you have ACCESS_NETWORK_STATE permission? - error: {0}", e);
                return(false);
            }
        }
        internal bool GetIsConnected(ConnectivityManager manager)
        {
            try
            {
                //When on API 21+ need to use getAllNetworks, else fall base to GetAllNetworkInfo
                //https://developer.android.com/reference/android/net/ConnectivityManager.html#getAllNetworks()
                if ((int)global::Android.OS.Build.VERSION.SdkInt >= 21)
                {
                    foreach (var network in manager.GetAllNetworks())
                    {
                        try
                        {
                            var capabilities = manager.GetNetworkCapabilities(network);

                            if (capabilities == null)
                            {
                                continue;
                            }

                            //check to see if it has the internet capability
                            if (!capabilities.HasCapability(NetCapability.Internet))
                            {
                                continue;
                            }

                            var info = manager.GetNetworkInfo(network);

                            if (info == null || !info.IsAvailable)
                            {
                                continue;
                            }

                            if (info.IsConnected)
                            {
                                return(true);
                            }
                        }
                        catch
                        {
                            //there is a possibility, but don't worry
                        }
                    }
                }
                else
                {
#pragma warning disable CS0618 // Type or member is obsolete
                    foreach (var info in manager.GetAllNetworkInfo())
#pragma warning restore CS0618 // Type or member is obsolete
                    {
                        if (info == null || !info.IsAvailable)
                        {
                            continue;
                        }

                        if (info.IsConnected)
                        {
                            return(true);
                        }
                    }
                }

                return(false);
            }
            catch (Exception e)
            {
                Logger.Warn("Unable to get connected state - do you have ACCESS_NETWORK_STATE permission? - error: {0}", e);
                return(false);
            }
        }
 public static NetworkInfo[] getAllNetworkInfo(this ConnectivityManager manager)
 {
     return(manager.GetAllNetworkInfo());
 }