I don\'t want my user to even try downloading something unless they have Wi-Fi connected. However, I can only seem to be able to tell if Wi-Fi is enabled, but they could sti
Similar to @Jason Knight answer, but in Kotlin way:
val connManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
if (mWifi.isConnected) {
// Do whatever
}
Kind of old a question but this is what i use. requires min api level 21 also takes in consideration deprecated Networkinfo apis.
boolean isWifiConn = false;
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Network network = connMgr.getActiveNetwork();
if (network == null) return false;
NetworkCapabilities capabilities = connMgr.getNetworkCapabilities(network);
if(capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)){
isWifiConn = true;
Toast.makeText(context,"Wifi connected Api >= "+Build.VERSION_CODES.M,Toast.LENGTH_LONG).show();
}else{
Toast.makeText(context,"Wifi not connected Api >= "+Build.VERSION_CODES.M,Toast.LENGTH_LONG).show();
}
} else {
for (Network network : connMgr.getAllNetworks()) {
NetworkInfo networkInfo = connMgr.getNetworkInfo(network);
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI && networkInfo.isConnected()) {
isWifiConn = true;
Toast.makeText(context,"Wifi connected ",Toast.LENGTH_LONG).show();
break;
}else{
Toast.makeText(context,"Wifi not connected ",Toast.LENGTH_LONG).show();
}
}
}
return isWifiConn;
Try out this method.
public boolean isInternetConnected() {
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean ret = true;
if (conMgr != null) {
NetworkInfo i = conMgr.getActiveNetworkInfo();
if (i != null) {
if (!i.isConnected()) {
ret = false;
}
if (!i.isAvailable()) {
ret = false;
}
}
if (i == null)
ret = false;
} else
ret = false;
return ret;
}
This method will help to find internet connection available or not.
I simply use the following:
SupplicantState supState;
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
supState = wifiInfo.getSupplicantState();
Which will return one of these states at the time you call getSupplicantState();
ASSOCIATED - Association completed.
ASSOCIATING - Trying to associate with an access point.
COMPLETED - All authentication completed.
DISCONNECTED - This state indicates that client is not associated, but is likely to start looking for an access point.
DORMANT - An Android-added state that is reported when a client issues an explicit DISCONNECT command.
FOUR_WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress.
GROUP_HANDSHAKE - WPA Group Key Handshake in progress.
INACTIVE - Inactive state.
INVALID - A pseudo-state that should normally never be seen.
SCANNING - Scanning for a network.
UNINITIALIZED - No connection.
I had a look at a few questions like this and came up with this:
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = connManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnected()){
// If Wi-Fi connected
}
if (mobile.isConnected()) {
// If Internet connected
}
I use if for my license check in Root Toolbox PRO, and it seems to work great.
While Jason's answer is correct, nowadays getNetWorkInfo (int) is a deprecated method. So, the next function would be a nice alternative:
public static boolean isWifiAvailable (Context context)
{
boolean br = false;
ConnectivityManager cm = null;
NetworkInfo ni = null;
cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
ni = cm.getActiveNetworkInfo();
br = ((null != ni) && (ni.isConnected()) && (ni.getType() == ConnectivityManager.TYPE_WIFI));
return br;
}