简体   繁体   中英

Android Internet Connection Check Failure

I am using following code to identify whether the phone is connected to internet or not.

public static boolean isOnline(Context context) {  
    try {
        ConnectivityManager cm = (ConnectivityManager) 
        context.getSystemService(Context.CONNECTIVITY_SERVICE);  
        return cm.getActiveNetworkInfo().isConnectedOrConnecting();
    } catch(Exception e) {
        return false;
    }
}

with this check, when phone is connected home wifi but note that there is no internet connection, still this method is returning me true. which is causing my application to crash as when it tries the code assuming internet is there, I can put further exception handling to prevent the crashes, but I need a better way to check internet connectivity which can return actual internet connection status...

Use this :

public  boolean isConnected() {
        boolean conectado;
        if (conectivtyManager.getActiveNetworkInfo() != null
                && conectivtyManager.getActiveNetworkInfo().isAvailable()
                && conectivtyManager.getActiveNetworkInfo().isConnected()) {
            conectado = true;
        } else {
            conectado = false;
        }
        return conectado;
    }

Place this permission:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

I always use:

public static boolean isOnline(Context ctx) {

    NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    if (info == null || !info.isConnected()) {
        return false;
    }
    if (info.isRoaming()) {
        // here is the roaming option you can change it if you want to
        // disable internet while roaming, just return false
        return false;
    }
    return true;
}

There's an extra check if the device is roaming, when true , it suggests that use of data on this network may incur extra costs.

Don't forget to add the right permission to your manifest outside the <application> tag:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

This worked for me:

public boolean networkCheckIn(){
try{

ConnectivityManager networkInfo = (ConnectivityManager) getSystemService(MapViewActivity.CONNECTIVITY_SERVICE) ;
networkInfo.getActiveNetworkInfo().isConnectedOrConnecting() ;

Log.d("1", "Net avail:"
                + networkInfo.getActiveNetworkInfo().isConnectedOrConnecting());

ConnectivityManager cm = (ConnectivityManager) getSystemService(MapViewActivity.CONNECTIVITY_SERVICE);
 NetworkInfo netInfo = cm.getActiveNetworkInfo();
 if (netInfo != null && netInfo.isConnectedOrConnecting()) {
    //Log.d("2", "Network available:true");
     return true;

  } else {
            //Log.d("3", "Network available:false");
            return false;
   }

}catch(Exception e){
        return false ;
}
}

Your problem is the method getActiveNetworkInfo() .

An example, to get network state of Wi-Fi:

public static boolean isWifiAvailable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null)
        return false;

    NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
    for (NetworkInfo ni : networkInfos) {
        if (ni.getType() == ConnectivityManager.TYPE_WIFI && ni.isConnected())
            return true;
    }
    return false;
}// isWifiAvailable()

There are more types in ConnectivityManager (they start with TYPE_* ).

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