Reputation: 163
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.
Upvotes: 6
Views: 9297
Reputation: 882
Settings.Global.DATA_ROAMING now throws SecurityException for target SDK 33 or above. For API 33 and above you can use TelephonyManager.isDataRoamingEnabled.
TelephonyManager.isDataRoamingEnabled requires one of the following permissions: Manifest.permission.ACCESS_NETWORK_STATE
or Manifest.permission.READ_PHONE_STATE
or Manifest.permission.READ_BASIC_PHONE_STATE
For more information: https://developer.android.com/reference/android/telephony/TelephonyManager#isDataRoamingEnabled()
public static boolean isDataRoamingEnabled(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
boolean hasFeature = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_DATA);
return hasFeature && telephonyManager.isDataRoamingEnabled();
} else {
final ContentResolver contentResolver = context.getContentResolver();
return android.provider.Settings.Global.getInt(
contentResolver,
android.provider.Settings.Global.DATA_ROAMING,
0) == 1;
}
}
Upvotes: 1
Reputation: 5618
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;
}
}
Upvotes: 4
Reputation: 1459
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;
}
}
Upvotes: 2
Reputation: 391
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;
}
}
Upvotes: 9
Reputation: 4739
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
Upvotes: 4