有关以编程方式启用和禁用移动数据的最新更新 [英] Latest update on enabling and disabling mobile data programmatically

查看:117
本文介绍了有关以编程方式启用和禁用移动数据的最新更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管这是重复的",但当前答案已过时,并且在大多数情况下不再适用.我认为最好在这里提供更新的资源,以节省人们的时间,就像我刚才所做的那样,研究这个问题.

Although this is a "duplicate", the current answers are out of date and, mostly, no longer apply. I thought it would be good to provide an updated resource here, if possible, to save people time, as I have just done, researching this issue.

我一直在四处寻找有关能够在应用程序中启用和禁用移动数据的最新信息(如果wifi不可用).

I've been googling around to see the latest information on being able to enable and disable mobile data from within an app (if wifi is not available).

这是我能找到的最新事物之一:
您是否知道无法再从小部件中禁用/启用棒棒糖上的数据了?/a>

This is one of the latest things I can find:
Did you know you can no longer Disable/Enable Data on lollipop from a widget?

对此我有一个答案:

从来没有针对它的API.开发人员正在使用一种变通办法,方法是通过Reflections调用该方法. Google所做的只是关闭此漏洞利用".

There was never an API for it. Developers were using a workaround by calling the method via Reflections. All Google did was close this "exploit".

这里还有讨论:

替换setMobileDataEnabled()API
这是2015年2月.

Replacement for setMobileDataEnabled() api
Which is Feb 2015.

这里有这些问题:

如何在Android上禁用移动数据

这是在2010年提出的,最新答案在2014年12月更新为一班.

This was asked in 2010 and the latest answer was updated with a one liner on Dec 2014.

以编程方式在android中启用/禁用数据连接

这是2012年被接受的答案.

And this, the accepted answer in 2012.

关于此问题的最新消息是什么?

What's the latest on this issue?

还可以吗?

推荐答案

我使用了一种变通方法,该变通方法仅适用于有根手机.

I use a workaround which only works for rooted phones.

已从ConnectivityManager中删除了setMobileDataEnabled方法,但为此功能在TelephonyManager中添加了两个方法getDataEnabledsetDataEnabled.

The setMobileDataEnabled method was removed from the ConnectivityManager but two methods getDataEnabled and setDataEnabled were added to the TelephonyManager for this functionality.

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

但是您需要将此权限( MODIFY_PHONE_STATE )添加到清单文件中,否则将收到SecurityException.

But you need to add this permission (MODIFY_PHONE_STATE) to the Manifest file otherwise you will get a SecurityException.

这篇关于有关以编程方式启用和禁用移动数据的最新更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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