I\'d like my android device to connect to a wifi hotspot.
I created a new wificonfiguration
and add it into the wifimanager
, this wificonfigu
You can try this:
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi.isConnected()) {
// Your code here
}
Edit: More details:
Register a BroadcastReceiver
in your manifest like so:
<receiver android:name="WifiReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
</receiver>
Then put the code above on the onReceive()
method of your receiver like so:
@Override
public void onReceive(Context context, final Intent intent) {
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi.isConnected()) {
// Your code here
}
}
getNetworkInfo(int) method is deprecated. You can apply something like this
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
if (activeNetwork != null) {
// connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
// connected to the mobile network
}
} else {
// not connected to the internet
}
Also, please add this permission under AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
This may help you .
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;
Log.i("ConnectionAvailable", "" + connectionavailable);
} catch (Exception e) {
System.out.println("Inside utils catch clause , exception is"
+ e.toString());
e.printStackTrace();
}
}
return haveConnectedWifi || haveConnectedMobile;
}
You can check all the network. If you only want WIFI you can remove checking other 2 network.
public static boolean hasInternetConnection()
{
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null && wifiNetwork.isConnected())
{
return true;
}
NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobileNetwork != null && mobileNetwork.isConnected())
{
return true;
}
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected())
{
return true;
}
return false;
}
Don't forget to add following in manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />