【发布时间】:2018-09-11 11:46:14
【问题描述】:
我正在尝试在 android lollipop 及更高版本中以编程方式切换 android 数据连接,但它不起作用并且总是出现异常。
这是我的代码
public void setMobileDataState(boolean mobileDataEnabled)
{
try
{
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method setMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
if (null != setMobileDataEnabledMethod)
{
setMobileDataEnabledMethod.invoke(telephonyService, mobileDataEnabled);
}
}
catch (Exception ex)
{
Log.e(TAG, "Error setting mobile data state", ex);
}
}
【问题讨论】:
-
有什么异常?
-
这些都低于 5.0 版。