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

前端之家收集整理的这篇文章主要介绍了Android L(5.x)以编程方式打开/关闭“移动数据”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要以编程方式打开/关闭移动数据.下面的代码不适用于5.x.你能帮我么.提前致谢.
  1. private void setMobileDataEnabled(Context context,boolean enabled) throws ClassNotFoundException,NoSuchFieldException,IllegalAccessException,NoSuchMethodException,InvocationTargetException {
  2. final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  3. final Class conmanClass = Class.forName(conman.getClass().getName());
  4. final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
  5. connectivityManagerField.setAccessible(true);
  6. final Object connectivityManager = connectivityManagerField.get(conman);
  7. final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName());
  8. final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled",Boolean.TYPE);
  9. setMobileDataEnabledMethod.setAccessible(true);
  10. 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.

  1. public void setMobileDataState(boolean mobileDataEnabled)
  2. {
  3. try
  4. {
  5. TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  6.  
  7. Method setMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("setDataEnabled",boolean.class);
  8.  
  9. if (null != setMobileDataEnabledMethod)
  10. {
  11. setMobileDataEnabledMethod.invoke(telephonyService,mobileDataEnabled);
  12. }
  13. }
  14. catch (Exception ex)
  15. {
  16. Log.e(TAG,"Error setting mobile data state",ex);
  17. }
  18. }
  19.  
  20. public boolean getMobileDataState()
  21. {
  22. try
  23. {
  24. TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  25.  
  26. Method getMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
  27.  
  28. if (null != getMobileDataEnabledMethod)
  29. {
  30. boolean mobileDataEnabled = (Boolean) getMobileDataEnabledMethod.invoke(telephonyService);
  31.  
  32. return mobileDataEnabled;
  33. }
  34. }
  35. catch (Exception ex)
  36. {
  37. Log.e(TAG,"Error getting mobile data state",ex);
  38. }
  39.  
  40. return false;
  41. }

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

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

猜你在找的Android相关文章