actually i need to implement a code for whether wifi internet available or not in my android mobile. i have a code, in this is internet connection available or not.please help me. Thanks
Asked
Active
Viewed 149 times
1
-
Duplcate: http://stackoverflow.com/questions/2802472/detect-network-connection-type-on-android – Laxmikant Ratnaparkhi Mar 14 '14 at 05:30
-
@Nand, your code is exactly the same as Squonk's code from : http://stackoverflow.com/questions/4238921/android-detect-whether-there-is-an-internet-connection-available?rq=1 – SunnyShiny9 Mar 14 '14 at 05:39
4 Answers
1
Try this-
final ConnectivityManager connMgr = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if( wifi.isAvailable() && wifi.getDetailedState() == DetailedState.CONNECTED){
Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
}
else if( mobile.isAvailable() && mobile.getDetailedState() == DetailedState.CONNECTED ){
Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();
}
use these permissions -
<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" />
ConnectivityManager Class that answers queries about the state of network connectivity.

T_V
- 17,440
- 6
- 36
- 48
0
try this
public static boolean haveNetworkConnection(Context context) {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}

Nand
- 614
- 4
- 10
0
Here's something that I got after some research.
This is for getting the Wifi State:
public static boolean getWifiState(Context context) {
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return mWifi.isConnected();
}
This is for getting the gprs :
public static boolean getNetworkType(Context context) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
int type = tm.getNetworkType();
//Here you can actually get some info on the network type. Try it out.
if (type >= TelephonyManager.NETWORK_TYPE_GPRS) {
return true;
}
return false;
}
Last thing is to add permissions:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK" />
Hope this helps..:)

mike20132013
- 5,357
- 3
- 31
- 41
0
You can use the following code :
public static boolean isDataEnabled(Context context)
{
return (isMobileDataEnabled(context) || isWifiEnabled(context));
}
public static boolean isMobileDataEnabled(Context context)
{
if(context == null)
return false;
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = null;
if (connectivityManager != null) {
networkInfo =
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
}
return networkInfo == null ? false : networkInfo.isConnected();
}
public static boolean isWifiEnabled(Context context)
{
if(context == null)
return false;
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = null;
if (connectivityManager != null) {
networkInfo =
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
}
return networkInfo == null ? false : networkInfo.isConnected();
}

Sushil
- 8,250
- 3
- 39
- 71