turtleboy
turtleboy

Reputation: 7572

How to detect if phone is scanning or connected to wifi

Certain users of my app are saying that the battery is getting drained quite quickly. I've determined that its when they have gps and wifi turned on.

I have the following code that i thought determined whether wifi was on or off. It always returns is not connected. For some reason my phone cannot connect to the office wifi, so that figures.

What i would like is to be able to detect if the wifi has been switched on in the phone settings.

I'm not sure if i'm correct but if the wifi is on but not connected, would this still drain the battery?

Is there any code that tells me if the wifi is switched on or off?

private static boolean isConnectedWiFi(Context context) {

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

        if (mWifi.isConnected()) {

            return true;

        } else {

            return false;
        }

}

Upvotes: 0

Views: 639

Answers (2)

Priyanka
Priyanka

Reputation: 675

Use this class for checking device is connected or not with working network. This class will work for all possible internet providers.

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context){
    this._context = context;
}

/**
 * Checking for all possible internet providers
 * **/
public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null)
      {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null)
              for (int i = 0; i < info.length; i++)
                  if (info[i].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }

      }
      return false;
}

}

// Connection detector

ConnectionDetector cd = new ConnectionDetector(getApplicationContext());

and call the method isConnectingToInternet() by object of ConnectionDetector class where you want to check internet connection. This method will return true when internet is working otherwise false.

// Check if Internet present
    if (!cd.isConnectingToInternet()) {
        // Internet Connection is not present
        alert.showAlertDialog(
                MainActivity.this,
                "Alert!!",
                "Internet Connection is not on.Please check your network.",
                false);
        // stop executing code by return
        return;
    }

Upvotes: 0

Manish Dubey
Manish Dubey

Reputation: 4306

This piece of code will help to check whether Wi-Fi is enabled or not on Android device.

WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()){
//wifi is enabled
}

You can also use wifi.getWifiState() for getting current state of wifi.

Upvotes: 1

Related Questions