Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check Internet Connection in Android for wifi connectivity?

Tags:

android

when wifi is connected to wireless modem internet coverage is there or not it always says yes you are conected it only checks wifi conectivity not internet connectivity so how to handle such a situation ?

like image 640
karthik Avatar asked May 30 '12 13:05

karthik


People also ask

How do you check if Wi-Fi is working on Android?

Tap Settings > Wireless & Networks > Wi-Fi as shown in Figure 1. If Wi-Fi is off, tap the slider to turn Wi-Fi on. When Wi-Fi is on, a signal indicator appears at the top right corner of your home screen.

How do I check Wi-Fi connection?

Go to Start , select Settings > Network & Internet, and see whether your wireless network name appears in the list of available networks. If you see your wireless network name, select it and select Connect.

How do I see all Wi-Fi networks on Android?

In your advanced Wi-Fi settings, select Manage networks. This is usually found beneath Network Settings. Now you'll see a list of all the Wi-Fi networks currently saved on your phone. To delete specific networks, tap them and select Forget.


2 Answers

1.)u can check it as

URL url = new URL("YOUR urlString");
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
.
.
int responseCode = conn.getResponseCode();
//if responseCode = 200 - THEn CONN is connected

OR

2.) u can do somethin like dis

public static boolean isNetworkAvailable(Activity activity) {
        ConnectivityManager connectivity = (ConnectivityManager) activity
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;  //<--  --  -- Connected
                    }
                }
            }
        }
        return false;  //<--  --  -- NOT Connected
    }
like image 131
Chintan Raghwani Avatar answered Oct 13 '22 10:10

Chintan Raghwani


Try this.

private boolean haveNetworkConnection(Context context)
    {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo)
        {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            {
                if (ni.isConnected())
                {
                    haveConnectedWifi = true;
                    Log.v("WIFI CONNECTION ", "AVAILABLE");
                } else
                {
                    Log.v("WIFI CONNECTION ", "NOT AVAILABLE");
                }
            }
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            {
                if (ni.isConnected())
                {
                    haveConnectedMobile = true;
                    Log.v("MOBILE INTERNET CONNECTION ", "AVAILABLE");
                } else
                {
                    Log.v("MOBILE INTERNET CONNECTION ", "NOT AVAILABLE");
                }
            }
        }
        return haveConnectedWifi || haveConnectedMobile;
    }

Hope it will help.

like image 34
Arun Badole Avatar answered Oct 13 '22 08:10

Arun Badole