Android的L(5.x的)开启/关闭和QUOT;移动数据"编程 [英] Android L (5.x) Turn ON/OFF "Mobile Data" programmatically

查看:1393
本文介绍了Android的L(5.x的)开启/关闭和QUOT;移动数据"编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以编程方式打开/关闭移动数据。下面code是不工作的5.x的你能帮我么。先谢谢了。

 私人无效setMobileDataEnabled(上下文的背景下,布尔启用)抛出ClassNotFoundException异常,NoSuchFieldException,IllegalAccessException,NoSuchMethodException,的InvocationTargetException {
        最后ConnectivityManager赌侠=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        最后类conmanClass =的Class.forName(conman.getClass()的getName());
        最后一个字段connectivityManagerField = conmanClass.getDeclaredField(MSERVICE);
        connectivityManagerField.setAccessible(真正的);
        最终目标connectivityManager = connectivityManagerField.get(赌侠);
        最后类connectivityManagerClass =的Class.forName(connectivityManager.getClass()的getName());
        最后一个方法setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod(setMobileDataEnabled,Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(真正的);
        setMobileDataEnabledMethod.invoke(connectivityManager,启用); }
 

  

十二月3日至30号:42:29.466:W / System.err的(5966):   java.lang.NoSuchMethodException:setMobileDataEnabled [布尔] 03-30   12:42:29.466:W / System.err的(5966):在   java.lang.Class.getMethod(Class.java:664)十二月3号至三十日:42:29.466:   W / System.err的(5966):在   java.lang.Class.getDeclaredMethod(Class.java:626)

java.lang.NoSuchMethodException:setMobileDataEnabled [布尔] // @以下行

  

最后一个方法setMobileDataEnabledMethod =   connectivityManagerClass.getDeclaredMethod(setMobileDataEnabled   Boolean.TYPE);

解决方案
  

这似乎是在 setMobileDataEnabled 方式不再存在    ConnectivityManager ,然后这个功能被移到    TelephonyManager 有两种方法getDataEnabled和setDataEnabled。

 公共无效setMobileDataState(布尔mobileDataEnabled)
{
    尝试
    {
        TelephonyManager telephonyService =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

        方法setMobileDataEnabledMethod = telephonyService.getClass()getDeclaredMethod(setDataEnabled,boolean.class)。

        如果(NULL!= setMobileDataEnabledMethod)
        {
            setMobileDataEnabledMethod.invoke(telephonyService,mobileDataEnabled);
        }
    }
    赶上(例外前)
    {
        Log.e(TAG,错误设置移动数据的状态,前);
    }
}

公共布尔getMobileDataState()
{
    尝试
    {
        TelephonyManager telephonyService =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

        方法getMobileDataEnabledMethod = telephonyService.getClass()getDeclaredMethod(getDataEnabled);

        如果(NULL!= getMobileDataEnabledMethod)
        {
            布尔mobileDataEnabled =(布尔)getMobileDataEnabledMethod.invoke(telephonyService);

            返回mobileDataEnabled;
        }
    }
    赶上(例外前)
    {
        Log.e(TAG,错误获取移动数据的状态,前);
    }

    返回false;
}
 

在执行code你得到一个SecurityException指出,无论是用户10089也不是当前进程android.permission.MODIFY_PHONE_STATE。

一个许可的 MODIFY_PHONE_STATE 应加 我从<一个得到这个href="http://stackoverflow.com/questions/26539445/the-setmobiledataenabled-method-is-no-longer-callable-as-of-android-l-and-later">Answer 谢谢Muzikant

I need to Turn ON/OFF Mobile data programmatically. Below code is not working for 5.x. Can you please help me. Thanks in advance.

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] @ below 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;
}

When executing the code you get a SecurityException stating that Neither user 10089 nor current process has android.permission.MODIFY_PHONE_STATE.

A permission MODIFY_PHONE_STATE should be added I got this from Answer Thank you Muzikant

这篇关于Android的L(5.x的)开启/关闭和QUOT;移动数据&QUOT;编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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