public void setMobileDataState(Context context, boolean enabled) {
TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Method setDataEnabled = telephonyService.getClass().getDeclaredMethod("setDataEnabled",boolean.class);
if (null != setDataEnabled) {
setDataEnabled.invoke(telephonyService, enabled);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean getMobileDataState(Context context) {
TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Method getDataEnabled = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
if (null != getDataEnabled) {
return (Boolean) getDataEnabled.invoke(telephonyService);
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
注意: 要使用系统签名, 否则权限不够
另特别注意, 有些支持双卡的手机, 在用此代码前要开过数据连接, 不然不知道是开哪张卡
没戏吧。5.0以后不能直接开数据连接了。