1

Now I am developing an application. I checked the internet connection in android using ConnectivManager. I checked for Mobile 3g and for WIFI.I turn off the WIFI connection and checked it works as what i want. Then I turn on the WIFI connection but disconnected all networks in WIFI. Now I checked but it shows the WIFI is connected but I want to check whether a network is connected to WIFI when WIFI is on or not connected to WIFI when WIFI is on. I do not know how to code it. Can anyone please help with the required code?

Thanks in advance

PM16
  • 73
  • 3
  • 9
  • Your question seems to have been already answered - http://stackoverflow.com/questions/3841317/how-to-see-if-wifi-is-connected-in-android – TwoA Apr 14 '13 at 05:16
  • Here is my answer from another post : http://stackoverflow.com/a/8548926/220710 – Emil Davtyan Apr 14 '13 at 06:33

4 Answers4

3
public boolean isWifiEnabled(){
       ConnectivityManager cm = (ConnectivityManager) c.getSystemService(
                            Context.CONNECTIVITY_SERVICE);
       NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

       if (wifiNetwork != null && wifiNetwork.isConnected()) {
          return true;
       }
    return false;
}

Don't forget to use the following permission

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • actually this is for checking whether the WIFI is connected or not.But what I want is to check whether any WIFI network is connected to WIFI – PM16 Apr 14 '13 at 05:59
  • No. I checked the WIFI is connected. I turn on the WIFI connection.But I disabled all wifi networks. Now I want to check whether WIFI is connected? If connected it has to check whether any WIFI network is connected? – PM16 Apr 14 '13 at 06:09
  • the first check whether the wifi is connected by using my method. then check for net connectivity. if net is not available then wifi network is not enabled .. (or may b i misunderstood your query) – stinepike Apr 14 '13 at 06:16
  • Actually am having three configured WIFI networks. I turn on the WIFI connection but disabled all those three network. Now I need to code for displaying that "WIFI connection with no network is connected" – PM16 Apr 14 '13 at 06:27
1

To Check if internet is connected via WiFi or mobile

public static boolean hasInternet(Activity a) {
 boolean hasConnectedWifi = false;
 boolean hasConnectedMobile = false;

 ConnectivityManager cm = (ConnectivityManager) a.getSystemService(Context.CONNECTIVITY_SERVICE);
 NetworkInfo[] netInfo = cm.getAllNetworkInfo();
 for (NetworkInfo ni : netInfo) {
  if (ni.getTypeName().equalsIgnoreCase("wifi"))
   if (ni.isConnected())
    hasConnectedWifi = true;
  if (ni.getTypeName().equalsIgnoreCase("mobile"))
   if (ni.isConnected())
    hasConnectedMobile = true;
 }
 return hasConnectedWifi || hasConnectedMobile;
}
Srikanth Pai
  • 926
  • 3
  • 17
  • 30
0

With this piece of code you should be able to use the ConnectivityManager. From there you can check if it is connected or even available.

        ConnectivityManager connManager = (ConnectivityManager)   getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (mWifi.isConnected()) {
            // Do whatever
        }
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

To check network connection

public class CheckNetwork {


private static final String TAG = CheckNetwork.class.getSimpleName();



public static boolean isInternetAvailable(Context context)
{
    NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
    context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

    if (info == null)
    {
         Log.d(TAG,"no internet connection");
         return false;
    }
    else
    {
        if(info.isConnected())
        {
            Log.d(TAG," internet connection available...");
            return true;
        }
        else
        {
            Log.d(TAG," internet connection");
            return true;
        }

    }
}
 }

In your activities

      if(Checknetwork.isInternetAvailable(MainActivity.this)
      {
          // do something
      }

Remember to add permission in manifest file

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

This also check's if you have connection to mobile network or wifi.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256