简体   繁体   中英

Checking internet connection issue

I am using this function to check if phone is connected to internet or not. But sometimes it shows connected even when disconnected. Can someone please have a look at it and let me know what I am doing wrong? Thanks in advance.

These are the functions I am using.

public String ConnectionQuality(Context context) {
    NetworkInfo info = getInfo(context);
    if (info == null || !info.isConnected()) {
        return "UNKNOWN";
    }

    if(info.getType() == ConnectivityManager.TYPE_WIFI) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        int numberOfLevels = 5;
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);
        if(level == 2 )
            return "POOR";
        else if(level == 3 )
            return "MODERATE";
        else if(level == 4 )
            return "GOOD";
        else if(level == 5 )
            return "EXCELLENT";
        else
            return "UNKNOWN";
    }else if(info.getType() == ConnectivityManager.TYPE_MOBILE) {
        int networkClass = getNetworkClass(getNetworkType(context));
        if(networkClass == 1)
            return "POOR";
        else if(networkClass == 2 )
            return "GOOD";
        else if(networkClass == 3 )
            return "EXCELLENT";
        else
            return "UNKNOWN";
    }else
        return "UNKNOWN";
}



public NetworkInfo getInfo(Context context) {
    return ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
}



public int getNetworkClass(int networkType) {
    try {
        return getNetworkClassReflect(networkType);
    }catch (Exception ignored) {
    }

    switch (networkType) {
        case TelephonyManager.NETWORK_TYPE_GPRS:
        case 16: // TelephonyManager.NETWORK_TYPE_GSM:
        case TelephonyManager.NETWORK_TYPE_EDGE:
        case TelephonyManager.NETWORK_TYPE_CDMA:
        case TelephonyManager.NETWORK_TYPE_1xRTT:
        case TelephonyManager.NETWORK_TYPE_IDEN:
            return 1;
        case TelephonyManager.NETWORK_TYPE_UMTS:
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
        case TelephonyManager.NETWORK_TYPE_HSDPA:
        case TelephonyManager.NETWORK_TYPE_HSUPA:
        case TelephonyManager.NETWORK_TYPE_HSPA:
        case TelephonyManager.NETWORK_TYPE_EVDO_B:
        case TelephonyManager.NETWORK_TYPE_EHRPD:
        case TelephonyManager.NETWORK_TYPE_HSPAP:
        case 17: // TelephonyManager.NETWORK_TYPE_TD_SCDMA:
            return 2;
        case TelephonyManager.NETWORK_TYPE_LTE:
        case 18: // TelephonyManager.NETWORK_TYPE_IWLAN:
            return 3;
        default:
            return 0;
    }
}

private int getNetworkClassReflect(int networkType) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Method getNetworkClass = TelephonyManager.class.getDeclaredMethod("getNetworkClass", int.class);
    if (!getNetworkClass.isAccessible()) {
        getNetworkClass.setAccessible(true);
    }
    return (Integer) getNetworkClass.invoke(null, networkType);
}

public static int getNetworkType(Context context) {
    return ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getNetworkType();
}

What's wrong with this code? Is there any better way to achieve same thing across all android versions?

i guess you are just checking the signal quality and not internet connection , to check if the phone is connected to internet or not use this function

 public boolean isOnline(Context context) {

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    //should check null because in airplane mode it will be null
    return (netInfo != null && netInfo.isConnected());

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM