Android L:数据关闭错误 [英] Android L: Data turn off error

查看:70
本文介绍了Android L:数据关闭错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在android 2.3-4.4.4上使用以下代码,没有任何错误.但是在android L开发人员预览版上运行它会给我以下错误

I've been using the following code on android 2.3 - 4.4.4 without any errors. But on running it on android L developer preview it gives me the following error

10-15 15:51:53.499:D/phone(30419):java.lang.NoSuchMethodException:setMobileDataEnabled [boolean]

10-15 15:51:53.499: D/phone(30419): java.lang.NoSuchMethodException: setMobileDataEnabled [boolean]

    try {
        // log.i("Application running on Ginger bread+");
        final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final Class<?> conmanClass = Class.forName(conman.getClass().getName());
        final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
        iConnectivityManagerField.setAccessible(true);
        final Object iConnectivityManager = iConnectivityManagerField.get(conman);
        final Class<?> iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
        final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);
        setMobileDataEnabledMethod.invoke(iConnectivityManager, ON);
    } catch (Exception e) {
        Log.d(TELEPHONY_SERVICE, e.toString());
    }

是否有针对此问题的解决方案.

Is there any work round for this problem.

感谢您的帮助萨希尔

推荐答案

setMobileDataEnabled 已在Android L中删除.

setMobileDataEnabled has been removed in Android L.

改为使用此:

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
Method methodSet = Class.forName(tm.getClass().getName()).getDeclaredMethod( "setDataEnabled", Boolean.TYPE);
methodSet.invoke(tm,true);

确保您对清单有此权限:

Make sure you have this permission on your manifest:

<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>

此权限仅适用于系统应用

这篇关于Android L:数据关闭错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆