java - 以编程方式打开移动数据在棉花糖上方的设备上不起作用
问题描述
我正在开发一个应用程序,我需要在 2 小时间隔后打开移动数据。但在棉花糖以上的设备中,它不起作用。我无法从应用程序切换移动数据。
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);
}
}
public boolean getMobileDataState()
{
try
{
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method getMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
if (null != getMobileDataEnabledMethod)
{
boolean mobileDataEnabled = (Boolean) getMobileDataEnabledMethod.invoke(telephonyService);
return mobileDataEnabled;
}
}
catch (Exception ex)
{
Log.e(TAG, "Error getting mobile data state", ex);
}
return false;
}
以上方法仅适用于有根设备。我也尝试过其他方法,但没有运气。如果有人做到了这一点。那么请提出一些很有帮助的建议。
解决方案
推荐阅读
- java - 时间戳数据库值在处理前显示为已处理
- matlab - 如何将此表达式转换为 MATLAB 代码
- android - android导航抽屉中的自定义图像按钮
- node.js - Google Cloud:使用 API 示例创建一个简单的应用程序 npm install failed
- java - Java 第一次尝试简单的计算器(发生了什么?)
- android - 如何获得高质量/完整图像并放入 imageview?
- ruby-on-rails - 如何使用 nginx、capistrano 和乘客部署 React 前端 + Rails api 后端
- firebase-cloud-messaging - Firebase 消息,未调用 onMessage
- mongodb - Spring Boot + MongoDB:如何重用连接
- animation - 在 Unity 中为动画精灵添加帽子或皮肤