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

noi*_*ion 5 android

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

小智 7

根据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;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 它是来自 API 17 的 _Settings.Global.DATA_ROAMING_。 (2认同)

Nip*_*pey 4

您可以通过以下方式请求漫游交换机的状态

ContentResolver cr = ContentResolver(getCurrentContext());
Settings.Secure.getInt(cr, Settings.Secure.DATA_ROAMING);
Run Code Online (Sandbox Code Playgroud)

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