[英]How to check programmatically if data roaming is enabled/disabled?
我正在嘗試檢查用戶是否已啟用/禁用數據漫游。 到目前為止我發現的只是你可以使用TelephonyManager.isNetworkRoaming()和NetworkInfo.isRoaming()檢查用戶當前是否正在漫游,但它們不是我需要的。
根據Nippey的回答,對我有用的實際代碼是:
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;
}
}
您可以通過請求Roaming-Switch的狀態
ContentResolver cr = ContentResolver(getCurrentContext());
Settings.Secure.getInt(cr, Settings.Secure.DATA_ROAMING);
請參閱: http : //developer.android.com/reference/android/provider/Settings.Secure.html#DATA_ROAMING
更新了API棄用帳戶的功能。 它現在替換為: 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;
}
}
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);
}
else
{
return (Settings.Global.getInt(APPLICATION_CONTEXT.getContentResolver(), Settings.Global.DATA_ROAMING, 0) == 1);
}
}
catch (Exception exception)
{
return false;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.