Android check if wifi enabled

Check whether mobile is using WIFI or Data/3G

Assuming that both WIFI and Data/3G are enabled on a device, how do I check if the user is currently using the internet over the wifi or the data plan assuming they are both enabled. So I don’t need to check if they are enabled or disabled, I would like to detect which one is the user actually using. I’ve been doing the following, is this the only solution?

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if (WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()) == NetworkInfo.DetailedState.CONNECTED)

4 Answers 4

void chkStatus() < 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.isConnectedOrConnecting ()) < Toast.makeText(this, "Wifi", Toast.LENGTH_LONG).show(); >else if (mobile.isConnectedOrConnecting ()) < Toast.makeText(this, "Mobile 3G ", Toast.LENGTH_LONG).show(); >else < Toast.makeText(this, "No Network ", Toast.LENGTH_LONG).show(); >> 

what is this downvote for please comment so I can work on my answers as far as I know I am very much correct

If it’s already been answered, please link to the question rather than plagiarizing the answer. Certainly the answer is correct — you copied it, after all. However, that just create duplicates on SO, making SO overall less usable.

Your answer is wrong. isAvailable() tells if network connectivity via wifi or mobile is possible, but don’t tell if there is actual connection. You must check isConnectedOrConnecting() or just isConnected().

thanks for pointing this out, I will update my answer but I believe it solved the ARMAGEDDON’s problem.

Источник

how to check wifi or 3g network is available on android device

Here, my android device supports both wifi and 3g. At particular time which network is available on this device. Because my requirement is when 3g is available I have to upload small amount of data. when wifi is available entire data have to upload. So, I have to check connection is wifi or 3g. Please help me. Thanks in advance.

4 Answers 4

/** * Checks if we have a valid Internet Connection on the device. * @param ctx * @return True if device has internet * * Code from: http://www.androidsnippets.org/snippets/131/ */ public static boolean haveInternet(Context ctx) < NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); if (info == null || !info.isConnected()) < return false; >if (info.isRoaming()) < // here is the roaming option you can change it if you want to // disable internet while roaming, just return false return false; >return true; > 

To get the network type you can use this code snippet:

ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); //mobile State mobile = conMan.getNetworkInfo(0).getState(); //wifi State wifi = conMan.getNetworkInfo(1).getState(); 

and then use it like that:

if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) < //mobile >else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) < //wifi >

To get the type of the mobile network I would try TelephonyManager#getNetworkType or NetworkInfo#getSubtypeName

Читайте также:  Установка драйвера wifi windows 10

You need to add below permission in android manifest file:

After that you can use below functions to check wifi or mobile network is connected or not

public static boolean isWifiConnected(Context context) < ConnectivityManager connManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); return ((netInfo != null) && netInfo.isConnected()); >public static boolean isMobileConnected(Context context)

Some references from developer.android.com are:

First get a reference to the ConnectivityManager and then check the Wifi and 3G status of the device. You’ll need the ACCESS_NETWORK_STATE permission to use this service.

 ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mWifi.isAvailable() == true) < return "Connected to WiFi"; >else if (mMobile.isAvailable() == true) < return "Connected to Mobile Network"; >else return "No internet Connection" 

Источник

Android : Check 3G or Wifi network is ON or Available or not on android Device

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.

10 Answers 10

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

Hope this will help someone.

Nullpointer in boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();

As @benoffi7 said, you have to test that manager.getNetworkInfo(ConnectivityManager.*) is not null before to call the isConnectedOrConnecting() method.

getInfoNetwork(int) is now deprecated. Just for the record (see this StackOverflow answer for an updated version).

getNetworkInfo() is depreciated now, because for example: in case, there are 2 cellular networks on device.

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

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

Читайте также:  Интернет вай фай велком

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; > 
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); String name networkInfo.getTypeName(); 

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

Use the following code as NetworkChecker.java & reuse it in your code:

package das.soumyadip.util; import android.net.ConnectivityManager; import android.util.Log; public class NetworkChecker < private final static String TAG = "NwtworkChecker"; public static boolean isNetworkConnected( final ConnectivityManager connectivityManager) < boolean val = false; Log.d(TAG, "Checking for Mobile Internet Network"); final android.net.NetworkInfo mobile = connectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mobile.isAvailable() && mobile.isConnected()) < Log.i(TAG, "Found Mobile Internet Network"); val = true; >else < Log.e(TAG, "Mobile Internet Network not Found"); >Log.d(TAG, "Checking for WI-FI Network"); final android.net.NetworkInfo wifi = connectivityManager .getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (wifi.isAvailable() && wifi.isConnected()) < Log.i(TAG, "Found WI-FI Network"); val = true; >else < Log.e(TAG, "WI-FI Network not Found"); >return val; > > 

Why 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?

 // TODO Auto-generated method stub ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); final android.net.NetworkInfo mobile = connectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mobile.isAvailable() && mobile.isConnected()) < Log.i(TAG, "Found Mobile Internet Network"); val = true; >// Checks the user prefs and the network connection. Based on the result, decides // whether // to refresh the display or keep the current display. // If the userpref is Wi-Fi only, checks to see if the device has a Wi-Fi connection. if (WIFI.equals(sPref) && networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) < // If device has its Wi-Fi connection, sets refreshDisplay // to true. This causes the display to be refreshed when the user // returns to the app. refreshDisplay = true; Toast.makeText(context, R.string.wifi_connected, Toast.LENGTH_SHORT).show(); // If the setting is ANY network and there is a network connection // (which by process of elimination would be mobile), sets refreshDisplay to true. >> else if (ANY.equals(sPref) && networkInfo != null) < refreshDisplay = true; // Otherwise, the app can't download content--either because there is no network // connection (mobile or Wi-Fi), or because the pref setting is WIFI, and there // is no Wi-Fi connection. // Sets refreshDisplay to false. >else

We can use ConnectivityManager Class for any information related to network.

Читайте также:  Санаторий коралл вай фай

It also notifies applications when network connectivity changes. Get an instance of this class by calling

  1. Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)
  2. Send broadcast intents when network connectivity changes
  3. Attempt to «fail over» to another network when connectivity to a network is lost
  4. Provide an API that allows applications to query the coarse-grained or fine-grained state of the available networks
  5. Provide an API that allows applications to request and select networks for their data traffic

GetNetworkInfo function return status information about a particular network type.

This method requires the caller to hold the permission

 /** * Checks the type of data connection that is currently available on the device. * * @return ConnectivityManager.TYPE_* as a type of internet connection on the *This method does not support multiple connected networks * of the same type. * device. Returns -1 in case of error or none of * ConnectivityManager.TYPE_* is found. **/ 
public static int getDataConnectionType(Context ctx) < ConnectivityManager connectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager != null && connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) != null) < if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected()) < return ConnectivityManager.TYPE_MOBILE; >else if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) < return ConnectivityManager.TYPE_WIFI; >else return -1; > else return -1; > 

Источник

Оцените статью
Adblock
detector