Search code examples
androidmobileerror-handlingwifi

Checking internet connection


I check the connection with

ConnectivityManager connec = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

i have wifi and i get wrong status on my cell phone what could i do ?


Solution

  • PLease use following standard method to check weather Internet connection is connected or not.....

    public static final boolean isNetworkConnected(Context context) {
            if (context != null) {
                ConnectivityManager mgr = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
    
                if (mgr != null) {
                    boolean mobileNetwork = false;
                    boolean wifiNetwork = false;
    
                    NetworkInfo mobileInfo = mgr
                            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                    NetworkInfo wifiInfo = mgr
                            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    
                    if (mobileInfo != null)
                        mobileNetwork = mobileInfo.isConnected();
                    if (wifiInfo != null)
                        wifiNetwork = wifiInfo.isConnected();
    
                    return (mobileNetwork || wifiNetwork);
                }
            }
            return false;
        }