In my application I just need to know if the device is connected to wifi network or not. I think this function works on emulator but not on real device.
public static boolean wifiInternet(Context c)
{
try
{
ConnectivityManager connectivityManager = (ConnectivityManager)c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = connectivityManager.getActiveNetworkInfo(); // CRASHES HERE
String name = ni.getTypeName();
if(name.equals("WIFI"))
return true;
else
return false;
}
catch(Exception e)
{
return false;
}
}
And which context do I use here? getAplicationContext() or getBaseContext() or do I just put 'this' (I'm calling the function from a Service).
Try this:
You can also use the same for the mobile type: