How to check programmatically if data roaming is on / off?

I am trying to check if the user is on / off data roaming. All I have found so far is that you can check if the user is currently roaming using TelephonyManager.isNetworkRoaming () and NetworkInfo.isRoaming (), but I do not need them.

+4
source share
4 answers

You can request the status of the Roaming switch through

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

+4
source

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; } } 
+6
source
 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; } } 
+3
source

Updated feature to account for obsolete APIs. Now it is replaced by: 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; } } 
+2
source

Source: https://habr.com/ru/post/1434701/


All Articles