场景:
个人开发的省电APP,有这么一个功能,可以快速打开GPRS数据,关闭GPRS数据,这算是必须的功能,放在之前,很好解决,但是现在,安卓越来越考虑用户体验和安全性,那么给非系统APP的权限就越来越少了!
问题来了:
从 Android 2.1 < API 7 >到 Android 4.4 < API 19 >setMobileDataEnabled()可以通过反射方式被调用了, 但是到了Android L以后 ,即使你有root权限, setMobileDataEnabled() method 也不能被调用,捷径没有了!
可以自己打印一下 看这个方法 还有没有
final Class<?> conmanClass = Class.forName(context.getSystemService(Context.CONNECTIVITY_SERVICE).getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(context.getSystemService(Context.CONNECTIVITY_SERVICE));
final Class<?> iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method[] methods = iConnectivityManagerClass.getDeclaredMethods();
for (final Method method : methods) {
if (method.toGenericString().contains(