gpt4 book ai didi

Android L (5.x) 以编程方式打开/关闭 "Mobile Data"

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:08 25 4
gpt4 key购买 nike

我需要以编程方式打开/关闭移动数据。以下代码不适用于 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] @ 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;
}

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

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

关于Android L (5.x) 以编程方式打开/关闭 "Mobile Data",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29340150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com