How to Check Wifi or 3G Network Is Available on Android Device

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

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.

How do I see if Wi-Fi is connected on Android?

You should be able to use the ConnectivityManager to get the state of the Wi-Fi adapter. From there you can check if it is connected or even available.

ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
// Do whatever
}

NOTE: It should be noted (for us n00bies here) that you need to add

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

to your

AndroidManifest.xml for this to work.

NOTE2: public NetworkInfo getNetworkInfo (int networkType) is now deprecated:

This method was deprecated in API level 23. This method does not
support multiple connected networks of the same type. Use
getAllNetworks() and getNetworkInfo(android.net.Network) instead.

NOTE3: public static final int TYPE_WIFI is now deprecated:

This constant was deprecated in API level 28.
Applications should instead use NetworkCapabilities.hasTransport(int) or requestNetwork(NetworkRequest, NetworkCallback) to request an appropriate network. for supported transports.

Android: How to check whether 3G is enabled or not?

Updated after test on LG GT540 phone:

You can use Settings.Secure to read preferred network mode, as in this code:

ContentResolver cr = getContentResolver();
int value = Secure.getInt(cr, "preferred_network_mode");

On my LG GT540 with CM 7.1 firmware, I have four options:

  • GSM/WCDM (auto) - the code above returns 3
  • WCDMA only - the code above returns 2
  • GSM only - the code above returns 1
  • GMS/WCDMA (WCMDA preferred) - the code above returns 0

Naturally, GSM is 2G and WCDMA is 3G. Note that this does not provide you with information on which connection is currently active (provided you allow both). For that, see @VikashKLumar's answer.

How detect networktype (2G/3G/LTE) when connected to WiFi

The issue was that in API-version 24 and forward getNetworkType() has been split in into two separate methods:

getDataNetworkType()

and

getVoiceNetworkType()

I added a check in my code to se if the returned type was IWLAN and if so use getVoiceNetworkType() instead.

if (tm.getNetworkType() != 18)
return tm.getNetworkType();
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
return tm.getVoiceNetworkType();
else
return -1;

how to check internet connectivity in the device in android

 public final boolean isInternetOn()
{

ConnectivityManager connec = (ConnectivityManager) getSystemService
(Context.CONNECTIVITY_SERVICE);

if ((connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)
||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING)
||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING)
||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED))

{

return true;

}

else
return false;

}

Android check internet connection

This method checks whether mobile is connected to internet and returns true if connected:

private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
}

in manifest,

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

Edit:
This method actually checks if device is connected to internet(There is a possibility it's connected to a network but not to internet).

public boolean isInternetAvailable() {
try {
InetAddress ipAddr = InetAddress.getByName("google.com");
//You can replace it with your name
return !ipAddr.equals("");

} catch (Exception e) {
return false;
}
}


Related Topics



Leave a reply



Submit