Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check programmatically if data roaming is enabled/disabled?

Tags:

android

I'm trying to check if the user has enabled/disabled data roaming. All I found so far is that you can check whether or not the user is currently IN roaming, using TelephonyManager.isNetworkRoaming() and NetworkInfo.isRoaming(), but they are not what I need.

like image 569
noillusion Avatar asked Sep 17 '12 20:09

noillusion


4 Answers

Based on Nippey's answer, the actual piece of code that worked for me is:

public Boolean isDataRoamingEnabled(Context context) {
    try {
        // return true or false if data roaming is enabled or not
        return Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.DATA_ROAMING) == 1;
    } 
    catch (SettingNotFoundException e) {
        // return null if no such settings exist (device with no radio data ?) 
        return null;
    }
}
like image 137
user1826063 Avatar answered Oct 23 '22 17:10

user1826063


You can request the state of the Roaming-Switch via

ContentResolver cr = ContentResolver(getCurrentContext());
Settings.Secure.getInt(cr, Settings.Secure.DATA_ROAMING);

See: http://developer.android.com/reference/android/provider/Settings.Secure.html#DATA_ROAMING

like image 25
Nippey Avatar answered Oct 23 '22 17:10

Nippey


public static final Boolean isDataRoamingEnabled(final Context application_context)
{
    try
    {
        if (VERSION.SDK_INT < 17)
        {
            return (Settings.System.getInt(application_context.getContentResolver(), Settings.Secure.DATA_ROAMING, 0) == 1);
        }

        return (Settings.Global.getInt(application_context.getContentResolver(), Settings.Global.DATA_ROAMING, 0) == 1);
    } 
    catch (Exception exception)
    {
        return false;
    }
}
like image 4
Yousha Aleayoub Avatar answered Oct 23 '22 16:10

Yousha Aleayoub


Updated function to account for API deprecation. It is now replaced with: http://developer.android.com/reference/android/provider/Settings.Global.html#DATA_ROAMING

public static boolean IsDataRoamingEnabled(Context context) {
    try {
        // return true or false if data roaming is enabled or not
        return Settings.Global.getInt(context.getContentResolver(), Settings.Global.DATA_ROAMING) == 1;
    } 
    catch (SettingNotFoundException e) {
        return false;
    }
}
like image 2
Kinman Avatar answered Oct 23 '22 17:10

Kinman