Reputation: 5166
I know how to check if wifi is enabled or not.
Code:
WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if(wifi.isWifiEnabled())
{
//Code execution comes here
}
But how to find out if the user is actually connected to a nearby wifi network (or any wifi network for that matter)?
EDIT: I mean to ask, say if a user has logged in to a wifi network after typing in a password, then only would he be able to use that wifi. So is there anyway to check if has connected (logged in) to any wifi network?
Upvotes: 1
Views: 239
Reputation: 5145
Just call this method for wifi network may this will help u :)
call like this checkWiFi(this);
// pass context
with this method here i pass this
for current activity
public static final boolean checkWiFi(Context cn)
{
ConnectivityManager connManager = (ConnectivityManager) cn.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected())
return true;
else
return false;
}
also take permission
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
Upvotes: 0
Reputation: 1684
You should be able to use the ConnectivityManager to get the state of the Wifi adapter. By this you can check if it is connected...
Method to check whether wifi is conected or not :-
public static Boolean checkWIFI(Activity activity) {
Log.d("checkWIFI", "checkWIFI");
ConnectivityManager cm = (ConnectivityManager) activity
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
Log.d("NetworkInfo", "NetworkInfo" + netInfo);
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else if (netInfo != null
&& (netInfo.getState() == NetworkInfo.State.DISCONNECTED
|| netInfo.getState() == NetworkInfo.State.DISCONNECTING
|| netInfo.getState() == NetworkInfo.State.SUSPENDED || netInfo
.getState() == NetworkInfo.State.UNKNOWN)) {
return false;
} else {
return false;
}
}
You need to add permission in your manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
Good Luck!!
Upvotes: 1
Reputation: 1039
/**
*
* Method to check internet connection is available
*/
public static boolean isInternetAvailable(Context context) {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
boolean connectionavailable = false;
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
NetworkInfo informationabtnet = cm.getActiveNetworkInfo();
for (NetworkInfo ni : netInfo) {
try {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
if (informationabtnet.isAvailable()
&& informationabtnet.isConnected())
connectionavailable = true;
} catch (Exception e) {
// TODO: handle exception
System.out.println("Inside utils catch clause , exception is"
+ e.toString());
e.printStackTrace();
}
}
return haveConnectedWifi || haveConnectedMobile;
}
I have tried the similar code in my project and is running.Hope this would help you...:)
Upvotes: 0
Reputation: 2365
Try this
ConnectivityManager con=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE);
boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
//check Internet connection
if(wifi)
{
//do what you want
}else{
}
also add require permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Hope this will do the trick
Upvotes: 0
Reputation: 11197
Try this:
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
// Do whatever
}
Hope this helps.. :)
Upvotes: 0