Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

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.

like image 629
Govardhan Reddy Avatar asked Jul 16 '10 07:07

Govardhan Reddy


People also ask

How do I see available Wi-Fi networks on Android?

Return to your Android device's Settings > Wireless & Networks > Wi-Fi panel and tap Wi-Fi Settings. Find your network's name (SSID) on the list of nearby Wi-Fi networks. If your network's name is not on the list, the AP or router may be hiding its SSID. Click Add Network to configure your network name manually.

Why is my Android not connecting to Wi-Fi?

If your Android phone isn't connecting to Wi-Fi, it may be that your router is acting up and not allowing any devices to connect to your wireless network. In this case, it's best to check your router's response using another Wi-Fi-enabled device. Try using another Android or any other device to connect to the network.


1 Answers

I use this:

/**  * 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; } 

You also need

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

in AndroidMainfest.xml

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

like image 107
Pentium10 Avatar answered Sep 20 '22 00:09

Pentium10