Android API call to determine user setting "Data Enabled"
Asked Answered
W

2

9

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.

Wistrup answered 6/7, 2011 at 5:4 Comment(0)
L
9

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

Lading answered 10/8, 2011 at 16:17 Comment(5)
Where can I find a list of all of the strings you can pass (i.e. "mobile_data") to Setttings.Secure?Marion
Officially, here and here. For the undocumented or hidden settings, you need to view the source of core/java/android/provider/Settings.java, which you can view here.Lading
Hi, is there a way to recieve some notification for change of settings , and only then read "mobile_data" ?Countermarch
@Countermarch I'm not sure. It'd probably be best to ask this as a separate question anyway.Lading
@Lading 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??Canvass
A
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;
    }
Apollonian answered 11/10, 2017 at 4:51 Comment(4)
it doesnt give excat result when you are recieving notification for wifi disabled..Canvass
it return false even when mobiledata is enabledCanvass
well, it works for me for all the devices. Interestingly, for the device you have not working. try other answers in that case. which device you have ?Apollonian
@ Mohammadhussain Khatri i have redmi3s primeCanvass

© 2022 - 2024 — McMap. All rights reserved.