大佬教程收集整理的这篇文章主要介绍了Android L(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); }
java.lang.NoSuchMethodException:setMobileDataEnabled [Boolean] @在line下面.
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.
以上是大佬教程为你收集整理的Android L(5.x)以编程方式打开/关闭“移动数据”全部内容,希望文章能够帮你解决Android L(5.x)以编程方式打开/关闭“移动数据”所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。