6

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

4

4 回答 4

8

根据 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;
    }
}
于 2013-02-21T08:27:02.920 回答
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

于 2012-09-18T09:21:21.053 回答
4
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;
    }
}
于 2015-12-10T10:11:34.230 回答
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;
    }
}
于 2014-11-18T20:26:24.347 回答