Android L(5.x)以编程方式打开/关闭“移动数据”

我需要以编程方式打开/关闭移动数据.下面的代码不适用于5.x.你能帮我么.提前致谢.
private void setMobileDataEnabled(Context context,boolean enabled) throws ClassNotFoundException,NoSuchFieldException,IllegalAccessException,NoSuchMethodException,InvocationTargetException {
        final ConnectivityManager conman = (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final Class conmanClass = Class.forName(conman.getClass().getName());
        final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
        connectivityManagerField.setAccessible(true);
        final Object connectivityManager = connectivityManagerField.get(conman);
        final Class connectivityManagerClass =  Class.forName(connectivityManager.getClass().getName());
        final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled",Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);
        setMobileDataEnabledMethod.invoke(connectivityManager,enabled);    }

03-30 12:42:29.466: W/System.err(5966):
java.lang.NoSuchMethodException: setMobileDataEnabled [boolean] 03-30
12:42:29.466: W/System.err(5966): at
java.lang.Class.getMethod(Class.java:664) 03-30 12:42:29.466:
W/System.err(5966): at
java.lang.Class.getDeclaredMethod(Class.java:626)

java.lang.NoSuchMethodException:setMobileDataEnabled [boolean] @在line下面.

final Method setMobileDataEnabledMethod =
connectivityManagerClass.getDeclaredMethod(“setMobileDataEnabled”,
Boolean.TYPE);

解决方法

It seems like the setMobileDataEnabled method no longer exists in
ConnectivityManager and this functionality was moved to
TelephonyManager with two methods getDataEnabled and setDataEnabled.

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;
}

执行代码时,您会收到SecurityException,指出用户10089和当前进程都没有android.permission.MODIFY_PHONE_STATE.

添加权限MODIFY_PHONE_STATE
我从Answer得到了这个谢谢Muzikant

相关文章

以下为个人理解,如错请评 CE: 凭据加密 (CE) 存储空间, 实际路径/data/user_ce/ DE: 设备加密 (DE) 存...
转载来源:https://blog.csdn.net/yfbdxz/article/details/114702144 用EventLog.writeEvent打的日志(或...
事件分发机制详解 一、基础知识介绍 1、经常用的事件有:MotionEvent.ACTION_DOWN,MotionEvent.ACTION...
又是好久没有写博客了,一直都比较忙,最近终于有时间沉淀和整理一下最近学到和解决的一些问题。 最近进...
Android性能优化——之控件的优化 前面讲了图像的优化,接下来分享一下控件的性能优化,这里主要是面向...
android的开源库是用来在android上显示gif图片的。我在网上查了一下,大家说这个框架写的不错,加载大的...