Jerielle
Jerielle

Reputation: 7520

How to identify if WiFi state is active using getNetworkInfo?

I am having some problem in my simple app. In my code I am using a deprecated code so I read this documentation and I don't know how can I get the wifi status using this function getNetworkInfo (Network network) Before my code looks like this to identify its state.

 boolean wifi = conman.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();

Can you help me with this?

In using this method getNetworkInfo (Network network) what parameter should I put in order to get the state of the wifi?

Upvotes: 1

Views: 184

Answers (2)

Anish Mittal
Anish Mittal

Reputation: 1182

Code:

ConnectivityManager manager = getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = manager.getActiveNetworkInfo();
boolean isWifi = (netInfo.getType() == ConnectivityManager.TYPE_WIFI);
boolean isConnected = (netInfo != null && netInfo.isConnectedOrConnecting());

DONE.

Upvotes: 1

Harsh Dev Chandel
Harsh Dev Chandel

Reputation: 763

 public static boolean isOnline(Context mContext) {
 ConnectivityManager cm = (ConnectivityManager)   mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo mWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (mWifi != null && mWifi.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

Upvotes: 1

Related Questions