How to check if the connected network has a data connection or not permanently while the application is running?

My app is completely network dependent, so I have to check if there is a data exchange between the app and my server and the connection loss popup is triggered and if it is not connected to Wi-Fi or mobile data and if the service connected has a slow connection. I am using the following code where I can find out if the device is connected to wifi or mobile data and get the connection type and also get the code to associate the connected mobile network type which is not working at all. please write the following code ...

 public void CheckInternet()
        {
            Plugin.Connectivity.CrossConnectivity.Current.ConnectivityChanged += delegate
            {
                if (!(Plugin.Connectivity.CrossConnectivity.Current.IsConnected))
                {
                    StartActivity(typeof(InternetCheckingView));
                }
                if (Plugin.Connectivity.CrossConnectivity.Current.IsConnected)
                {
                    ConnectivityManager connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService);
                    NetworkInfo networkInf = connectivityManager.ActiveNetworkInfo;
                    bool isOnline = networkInf.IsConnected;
                    NetworkInfo networkInfo = connectivityManager.ActiveNetworkInfo;
                    bool isWifi = networkInfo.Type == ConnectivityType.Wifi;
                    bool isdata = networkInfo.Type == ConnectivityType.Mobile;
                    if (!isOnline)
                    { StartActivity(typeof(InternetCheckingView)); }
                    else if(isWifi)
                    {
                        Toast.MakeText(_context, "Wifi Connected", ToastLength.Short).Show();
                    }
                    else if (isdata)
                    {
                        Toast.MakeText(_context, "Mobile Data Connected", ToastLength.Short).Show();
                        bool datatype = IsConnectionFast(subType);
                        if(!(datatype))
                        {StartActivity(typeof(InternetCheckingView));}
                    }
                }
            };
        }

        public bool IsConnectionFast(object networkType)
        {
            switch (subType)
            {
                //case TelephonyManager.NETWORK_TYPE_1xRTT:
                case NetworkType.OneXrtt:
                    return false; // ~ 50-100 kbps//case TelephonyManager.NETWORK_TYPE_CDMA:
                case NetworkType.Cdma:
                    return false; // ~ 14-64 kbps//case TelephonyManager.NETWORK_TYPE_EDGE:
                case NetworkType.Edge:
                    return false; // ~ 50-100 kbps//case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case NetworkType.Evdo0:
                    return true; // ~ 400-1000 kbps //case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case NetworkType.EvdoA:
                    return true; // ~ 600-1400 kbps//case TelephonyManager.NETWORK_TYPE_GPRS:
                case NetworkType.Gprs:
                    return false; // ~ 100 kbps//case TelephonyManager.NETWORK_TYPE_HSDPA:
                case NetworkType.Hsdpa:
                    return true; // ~ 2-14 Mbps//case TelephonyManager.NETWORK_TYPE_HSPA:
                case NetworkType.Hspa:
                    return true; // ~ 700-1700 kbps//case TelephonyManager.NETWORK_TYPE_HSUPA:
                case NetworkType.Hsupa:
                    return true; // ~ 1-23 Mbps//case TelephonyManager.NETWORK_TYPE_UMTS:
                case NetworkType.Umts:
                    return true; // ~ 400-7000 kbps
                                 /*
                                  * Above API level 7, make sure to set android:targetSdkVersion
                                  * to appropriate level to use these
                                  */
                                 //case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
                case NetworkType.Ehrpd:
                    return true; // ~ 1-2 Mbps//case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
                case NetworkType.EvdoB:
                    return true; // ~ 5 Mbps//case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
                case NetworkType.Hspap:
                    return true; // ~ 10-20 Mbps//case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
                case NetworkType.Iden:
                    return false; // ~25 kbps//case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
                case NetworkType.Lte:
                    return true; // ~ 10+ Mbps// Unknown//case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                case NetworkType.Unknown:
                    return false;
                default:
                    return false;
            }
        }

      

Here I would like to detect network is slow or not and if it is slow network I need to call StartActivity (typeof (InternetCheckingView))

Can anyone help me to do this, I would like to open a popup whenever there is no data connection available like onclicklisner for example, so that even filterers when there is data loss even when the network is connected

(new -)

I check the data transfer by typing the url when the network connection changed, like disconnected or changed between wifi and mobile data, but I would like to know a solution to get a popup to connect the network whenever data transfer is lost even if it is only connected to the network when the app is opened.

+3


source to share





All Articles