Search code examples
androidandroid-wireless

Android API call to determine user setting "Data Enabled"


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.


Solution

  • 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