8

My program tries to detect whether a mobile network is available at a certain location.

The issue is that when I don't have a data connection it doesn't mean the network is not there... it depends on the user preferences. There are APIs available for NetworkInfo.isAvailable(), and for user settings such as whether user is roaming and roaming is enabled or whether AirplaneMode is on.

My problem is that I can't figure out whether the user has data services disabled under Settings/WirelessNetworks/MobileNetworks.

Sounds like a trivial problem but I haven't found an API call.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rick
  • 115
  • 1
  • 2
  • 5

2 Answers2

8

In your activity:

boolean mobileDataAllowed = Settings.Secure.getInt(getContentResolver(), "mobile_data", 1) == 1;

Source: https://github.com/yanchenko/quick-settings/blob/master/src/com/bwx/bequick/handlers/MobileDataSettingHandler2.java#L123

bmaupin
  • 14,427
  • 5
  • 89
  • 94
  • Where can I find a list of all of the strings you can pass (i.e. "mobile_data") to Setttings.Secure? – gonzobrains May 22 '13 at 18:51
  • Officially, [here](http://developer.android.com/reference/android/provider/Settings.Secure.html) and [here](http://developer.android.com/reference/android/provider/Settings.Global.html). For the undocumented or hidden settings, you need to view the source of core/java/android/provider/Settings.java, which you can view [here](http://omapzoom.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/provider/Settings.java;hb=HEAD). – bmaupin May 23 '13 at 23:20
  • Hi, is there a way to recieve some notification for change of settings , and only then read "mobile_data" ? – ransh Jan 24 '17 at 12:08
  • @ransh I'm not sure. It'd probably be best to ask this as a separate question anyway. – bmaupin Jan 24 '17 at 12:58
  • @bmaupin it gives me false when im receiving notification for wifi disabled but my mobile data button has been enabled y is it still working lik u mentioned?? – Jeeva Jan 18 '18 at 12:45
1

I know above answer worked for OP. But in few devices I found it returns true even if data is disabled. So I found one alternate solution which is in Android API.

getDataState() method of TelephonyManager will be very useful.

Below function returns false when cellular data is disabled otherwise true.

private boolean checkMobileDataIsEnabled(Context context){
        boolean mobileYN = false;

        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (tm.getSimState() == TelephonyManager.SIM_STATE_READY) {
            TelephonyManager tel = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//          if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
//          {
//              mobileYN = Settings.Global.getInt(context.getContentResolver(), "mobile_data", 0) == 1;
//          }
//          else{
//              mobileYN = Settings.Secure.getInt(context.getContentResolver(), "mobile_data", 0) == 1;
//          }
            int dataState = tel.getDataState();
            Log.v(TAG,"tel.getDataState() : "+ dataState);
            if(dataState != TelephonyManager.DATA_DISCONNECTED){
                mobileYN = true;
            }

        }

        return mobileYN;
    }
MohK
  • 1,873
  • 1
  • 18
  • 29