定义“mobile_data”行名,系统在本地存储数据连接复选框/开关的状态:
private static final String MOBILE_DATA = "mobile_data";
并检查该行的值是否为0
。如果是0
,则数据连接未选中/禁用(未选中):
private boolean isMobileDataChecked() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
// The row has been moved to 'global' table in API level 17
return Settings.Global.getInt(getContentResolver(), MOBILE_DATA, 0) != 0;
}
try {
// It was in 'secure' table before
int enabled = Settings.Secure.getInt(getContentResolver(), MOBILE_DATA);
return enabled != 0;
} catch (SettingNotFoundException e) {
// It was in 'system' table originally, but I don't remember when that was the case.
// So, probably, you won't need all these try/catches.
// But, hey, it is better to be safe than sorry :)
return Settings.System.getInt(getContentResolver(), MOBILE_DATA, 0) != 0;
}
}