-5

How can I get connection is available or not on Android?

I tried lots of code, but I did not get the correct result.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Kavita
  • 7
  • 5

3 Answers3

3

Put in this function and call it at the starting of your Activity:

void checkInternetConnectionStatus()
{
    ConnectivityManager connMgr = (ConnectivityManager) this
        .getSystemService(Context.CONNECTIVITY_SERVICE);

    android.net.NetworkInfo wifi = connMgr
        .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    android.net.NetworkInfo mobile = connMgr
        .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (wifi.isAvailable()) {
        /*
         * Toast.makeText(this, "Wi-Fi connection enabled",
         * Toast.LENGTH_LONG) .show();
         */
    } else if (mobile.isAvailable()) {
        /*
         * Toast.makeText(this, "Mobile Internet enabled",
         * Toast.LENGTH_LONG) .show();
         */

    } else {
        Toast.makeText(this, "No Internet Connection", Toast.LENGTH_LONG)
            .show();

        new AlertDialog.Builder(this)
            .setTitle("No internet connection active")
            .setMessage("Please start internet connection and run this application.")
            .setNegativeButton(
                "Exit",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("AlertDialog", "Negative");
                        finish();
                    }
                }).show();
    }
}
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Dhaval Patel
  • 694
  • 4
  • 12
2

If you are trying to check for an Internet connection then use this:

public boolean isNetworkAvailable() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();

    // If no network is available networkInfo will be null,
    // otherwise check if we are connected.
    if (networkInfo != null && networkInfo.isConnected()) {
        return true;
    }
    return false;
}
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Piyush
  • 24,288
  • 6
  • 40
  • 72
  • Instead of giving answer you should give links and stop this kind of duplication of same question – SilentKiller Aug 14 '13 at 12:23
  • I tried this public boolean WifiConnected() { ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); return (networkInfo != null && networkInfo.isConnected()); } but it always return false – Kavita Aug 14 '13 at 12:40
  • 1
    have you add permission of android.permission.ACCESS_NETWORK_STATE to your manifest file?? and also for – Piyush Aug 14 '13 at 12:41
  • added these permission too But always gives me false value if its connected then also give me false – Kavita Aug 14 '13 at 13:05
  • How did you check this?? – Piyush Aug 14 '13 at 13:07
  • m testing on emulator. May be because of this? dont know – Kavita Aug 14 '13 at 13:15
  • Yes it is.....possible,..try to run on device and check ..i think it should be.... yet get problem then send me demo... – Piyush Aug 14 '13 at 13:17
  • yes it was the problem of emulator – Kavita Aug 15 '13 at 06:05
  • Ok...nice so..perfectly getting right?? – Piyush Aug 15 '13 at 06:07
1

Maybe this will help:

 ConnectivityManager conn = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo wifi = conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (wifi.isConnected()) {
    // Do your code
}

And also you need to add in AndroidManifest.xml:

android.permission.ACCESS_NETWORK_STATE

And also this link will help:

NetworkInfo

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Hariharan
  • 28,756
  • 7
  • 51
  • 55