setMobileDataEnabled方法不能再被调用 Android L and later

在Android Lollipop及更高版本中,非系统应用无法通过反射调用setMobileDataEnabled()来开启或关闭移动数据。尽管可以通过Settings.Global.getInt检查数据状态,但在没有ROOT权限的情况下,主动切换GPRS变得困难。对于系统应用,可以使用TelephonyManager的getDataEnabled和setDataEnabled方法,或者在ROOT设备上通过shell命令设置Settings.Global的mobile_data属性来实现。
摘要由CSDN通过智能技术生成

场景:
个人开发的省电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(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值