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

此生再无相见时 提交于 2019-12-04 18:29:10

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.

you can check the 3G by using

boolean is3G3 = (telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSUPA);
boolean is3G2 = (telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSPA);
boolean is3G = (telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSDPA);

These networks are 3G networks.

Just dial ×#×#4636#×#× go into phone information and see down if there is GSM only your phone would not support 3G and if It's WCDMA prefferd your phone can use 3G

/**
 * 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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!