Android : Check 3G or Wifi network is ON or Available or not on android Device
Solution 1
TO check whether network i.e 3G or WiFi is available or not we can use below methods to verify before starting our activities.
ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
//For 3G check
boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.isConnectedOrConnecting();
//For WiFi Check
boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.isConnectedOrConnecting();
System.out.println(is3g + " net " + isWifi);
if (!is3g && !isWifi)
{
Toast.makeText(getApplicationContext(),"Please make sure your Network Connection is ON ",Toast.LENGTH_LONG).show();
}
else
{
" Your method what you want to do "
}
Hope this will help someone.
Solution 2
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();
}
this code check if you are with wifi or 3g or nothing , in the case of wifi on but not connected to a net or 3g have signal problem it detect this details, with DetailedStates
Solution 3
You can use this method to check whether your internet connection is 2G, 3G or 4G:
public String getNetworkClass(Context context) {
TelephonyManager mTelephonyManager = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = mTelephonyManager.getNetworkType();
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_GPRS:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return "2G";
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
return "3G";
case TelephonyManager.NETWORK_TYPE_LTE:
return "4G";
default:
return "Unknown";
}
}
And using following method you can check if internet is available or not, and get whether you are accessing the internet via a mobile network or WiFi:
public String getNetworkType(Context context){
String networkType = null;
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
networkType = "WiFi";
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
networkType = "Mobile";
}
} else {
// not connected to the internet
}
return networkType;
}
Solution 4
This work for me
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
String name networkInfo.getTypeName();
Solution 5
Rahul Baradia's answer includes manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
and it's deprecated.
Below is an improved solution for the latest Android SDK.
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
boolean is3gEnabled = false;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Network[] networks = connManager.getAllNetworks();
for(Network network: networks)
{
NetworkInfo info = connManager.getNetworkInfo(network);
if(info!=null) {
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
is3gEnabled = true;
break;
}
}
}
}
else
{
NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(mMobile!=null)
is3gEnabled = true;
}
Rahul Baradia
Working as a Android developer.. For more informations, You can contact me on LinkedIn. Personal profile link: Rahul Baradia (http://rahulbaradia.com).
Updated on August 01, 2022Comments
-
Rahul Baradia almost 2 years
How to check that network is available or not on android device programmatically, which throws a message or toast message when we are trying to connect with a network such as Wifi & 3G.
-
Rahul Baradia almost 12 yearsrahulbaradiaa.blogspot.in/2012/07/… u can get the information here too..
-
benoffi7 over 10 yearsNullpointer in boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
-
Denis Gladkiy over 10 yearsWhy the switched off wifi (or 3G) is an error (Log.e(TAG, "WI-FI Network not Found")) 0_0? It is a valid case for a phone and for many apps that can do work (maybe part of it) without it. Why so many logging in such utility and not mission-critical code?
-
Kikiwa about 9 yearsAs @benoffi7 said, you have to test that manager.getNetworkInfo(ConnectivityManager.*) is not null before to call the isConnectedOrConnecting() method.
-
Anwar over 7 years
getInfoNetwork(int)
is now deprecated. Just for the record (see this StackOverflow answer for an updated version). -
HungNM2 over 7 yearsgetNetworkInfo() is depreciated now, because for example: in case, there are 2 cellular networks on device.
-
kemdo about 6 yearsthis return only that connected to network type, not return the type that enabled by user.