user903601
user903601

Reputation:

Android internet connection test

I have the following code that checks to see if there is an internet connection before calling the AsyncTask method, “Task” which then retrieves information from the internet. It only really seems to work if the phone is in flight mode or, if the phone is not connected to external wireless internet, i.e.. not working on its own internet that comes with the phones plan.

If the phone is connected to an external wireless modem but the modem is not connected to the internet I get a force close!

 if (isOnline()) {
    new Task().execute();
 } else {
     Toast.makeText(this, "There seems to be no internet access, please try again later!", Toast.LENGTH_LONG).show();
 }

And

 public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
        return false;
 }

Does anyone have a "tried and true" method to get around this?

Cheers,

Mike.

Upvotes: 4

Views: 1746

Answers (4)

user1140574
user1140574

Reputation:

check this code snippet

try {

    ConnectivityManager nInfo = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    nInfo.getActiveNetworkInfo().isConnectedOrConnecting();

    Log.d(tag, "Net avail:"
            + nInfo.getActiveNetworkInfo().isConnectedOrConnecting());

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        Log.d(tag, "Network available");
        return true;
    } else {
        Log.d(tag, "Network not available");
        return false;
    }

} catch (Exception e) {
    return false;
}

make sure that you have written following permission details in android-manifest.xml

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

check following question for more details android network connectivity

Upvotes: 4

Shishir Shetty
Shishir Shetty

Reputation: 2049

Check this out:

ConnectivityManager cm,cm1;




private static boolean isConnected(Context context) {

        NetworkInfo networkInfo = null;

        if (cm != null) {
            networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        }
        return networkInfo == null ? false : networkInfo.isConnected();
    }

    private static boolean isConnected1(Context context) {

        NetworkInfo networkInfo1 = null;

        if (cm1 != null) {

            networkInfo1 = cm1.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        }
        return networkInfo1 == null ? false : networkInfo1.isConnected();
    }

Upvotes: 0

Code_Life
Code_Life

Reputation: 5892


No its not like that it works properly with every mode . I tried myself using this

public boolean isOnline() {
    boolean flag = false;
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm.getActiveNetworkInfo() != null) {
        flag = cm.getActiveNetworkInfo().isConnectedOrConnecting();
    }
    return flag;
}

Upvotes: 0

i use to check one more condition in If case

 netInfo .isAvailable()

I think this is the only way , we can know whether internet is available or not.

Upvotes: 8

Related Questions