如何启用/禁用数据漫游以编程方式检查?

时间:2012-09-17 20:47:29

标签: android

我正在尝试检查用户是否已启用/禁用数据漫游。到目前为止我发现的只是你可以使用TelephonyManager.isNetworkRoaming()和NetworkInfo.isRoaming()来检查用户当前是否正在漫游,但它们不是我需要的。

4 个答案:

答案 0 :(得分:6)

根据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;
    }
}

答案 1 :(得分:4)

您可以通过

请求漫游切换的状态
ContentResolver cr = ContentResolver(getCurrentContext());
Settings.Secure.getInt(cr, Settings.Secure.DATA_ROAMING);

请参阅:http://developer.android.com/reference/android/provider/Settings.Secure.html#DATA_ROAMING

答案 2 :(得分:2)

更新了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;
    }
}

答案 3 :(得分:2)

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;
    }
}