2

I need to know whether 3G connectivity is permitted on the device or not. I don't want to know what current network state is, because if you set "Network Mode" setting in "Mobile network settings" screen to "Automatic" network state could be either 2G or 3G. I just want to know which setting is selected there - 2G, 3G or Automatic (latter two mean the same for me).

Both telephonyManager.getNetworkType() and ConnectivityManage.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState()

are returning current network state, which can lead me in a wrong direction, because if current state is 2G it could mean that 3G is disabled or just that 3G mode is unavailable at the specific location.

Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
Darth Ch
  • 23
  • 1
  • 1
  • 3

4 Answers4

3

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.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • Thank you, I have read it - unfortunately there is no answer for my question :( Settings.Secure doesn't have such an option – Darth Ch Dec 06 '12 at 13:00
  • @DarthCh What do you mean? How about [this field](http://developer.android.com/reference/android/provider/Settings.Secure.html#NETWORK_PREFERENCE) then? – Aleks G Dec 06 '12 at 13:33
  • 1
    Sorry but on my Acer Liquid this code int value = Settings.Secure.getInt(getContentResolver(), Settings.Secure.NETWORK_PREFERENCE); String s = Settings.Secure.getString(getContentResolver(), Settings.Secure.NETWORK_PREFERENCE); always returns 1 and "1", whether I choose 2G, 3G, Automatic or even disable the phone module. – Darth Ch Dec 06 '12 at 13:50
  • @DarthCh I created a test and ran it on my phone. After some playing around with the prefs, I found the correct value to query: it's _"preferred_network_mode"_. I now updated my answer with the working/tested code. – Aleks G Dec 06 '12 at 14:19
  • Thank you very much, on my Acer Liquid things are quite similar. Auto = 0, 3g only = 2, 2G only = 1. So I suppose I should make difference between 1 and any other value to check if the phone in "2G only" mode or 3G is allowed. – Darth Ch Dec 06 '12 at 16:45
  • @DarthCh If you have a chance, I'd test on a few other devices, specifically from other manufacturers. Most likely, the same manufacturer will use the same values on different devices; somewhat possible that different manufacturers could use different values, although not very likely. – Aleks G Dec 06 '12 at 17:02
1

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.

Vikash Kumar
  • 621
  • 2
  • 6
  • 21
  • 1
    I don't think this answers the question. The OP wants to check whether 3G is enabled rather than whether the current connection is 3G. He needs to retrieve network settings, not network state. – Aleks G Dec 06 '12 at 12:35
  • For that one need to use ITelephony class which is hidden API in Android. Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony"); – Vikash Kumar Dec 06 '12 at 12:43
  • And what should I do next, after obtaining that class? – Darth Ch Dec 06 '12 at 13:22
  • 1
    I want to know whether my device is 3G enabled or not? – Shihab Uddin Oct 31 '13 at 21:46
0

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

-1
/**
 * 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
Nipun Gogia
  • 1,846
  • 1
  • 11
  • 17
  • That's rather lame: copy an answer to another question and repost as your own. This was copied from here: http://stackoverflow.com/questions/3262781/how-to-check-wifi-or-3g-network-is-available-on-android-device - and, in addition, doesn't address the OP's question at all. The question is about voice 2G/3G and not about whether there's 3G data connection available. – Aleks G Dec 06 '12 at 12:25