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;
}
}
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;
}
}
5条答案
按热度按时间oknwwptz1#
根据Nippey的回答,对我有效的代码片段是:
g0czyy6m2#
您可以通过请求漫游交换机的状态
参见:http://developer.android.com/reference/android/provider/Settings.Secure.html#DATA_ROAMING
50few1ms3#
f0brbegy4#
已更新函数以说明API弃用。现在将其替换为:http://developer.android.com/reference/android/provider/Settings.Global.html#DATA_ROAMING
q43xntqr5#
设置.全局.DATA_ROAMING现在为目标SDK 33或更高版本引发SecurityException。对于API 33和更高版本,您可以使用TelephonyManager.isDataRoamingEnabled。
TelephonyManager.isDataRoamingEnabled需要下列权限之一:
Manifest.permission.ACCESS_NETWORK_STATE
或Manifest.permission.READ_PHONE_STATE
或Manifest.permission.READ_BASIC_PHONE_STATE
如需了解更多信息:https://developer.android.com/reference/android/telephony/TelephonyManager#isDataRoamingEnabled()