Amol
Amol

Reputation: 285

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...

Upvotes: 3

Views: 1620

Answers (4)

user1521536
user1521536

Reputation:

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_*).

Upvotes: 0

Timmetje
Timmetje

Reputation: 7694

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" />

Upvotes: 0

Guilherme Gregores
Guilherme Gregores

Reputation: 1048

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"/>

Upvotes: 0

Husnain Iqbal
Husnain Iqbal

Reputation: 466

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 ;
}
}

Upvotes: 0

Related Questions