如何在android中单击按钮时断开移动数据

时间:2018-06-06 07:20:52

标签: android mobile-data

我想在点击按钮时禁用移动daata。我试过下面的代码,但它不起作用。请帮我解决问题。提前致谢。

LayoutInflater

我得到这样的错误:

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);
}
}
enter code here

0 个答案:

没有答案