简体   繁体   中英

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;
        }

}

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.

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM