
我需要我的应用重置设备的移动网络信号.这与切换飞行模式时具有相同的效果,在该模式中,连接暂时中断,在重新连接时分配了新的IP地址,状态栏中的LTE /信号图标应消失,然后在重新连接时重新出现.我在Play商店中发现了an app,我在运行带有CyanogenMod的AndroID 4.4.4的手机上进行了测试,它确实做到了这一点,但是我不确定如何在自己的应用中实现此功能.我认为这与CHANGE_NETWORK_STATE权限有关.我正在寻找文档或一些简单的示例代码来重置网络连接.
请注意,我并不是专门尝试切换飞行模式,而是以上面链接的应用程序那样的方式重置移动数据,因为我已经测试了它确实可以工作,甚至不需要root特权.
解决方法:
棒棒糖支持需要新的系统级别权限androID.permission.MODIFY_PHONE_STATE才能工作.
private static boolean setMobileConnectionEnabled(Context context, boolean enabled){ try{ // Requires: androID.permission.CHANGE_NETWORK_STATE if(Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD){ // pre-Gingerbread sucks! final TelephonyManager telMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); final Method getITelephony = telMgr.getClass().getDeclaredMethod("getITelephony"); getITelephony.setAccessible(true); final Object objITelephony = getITelephony.invoke(telMgr); final Method toggleDataConnectivity = objITelephony.getClass() .getDeclaredMethod(enabled ? "enableDataConnectivity" : "DisabledataConnectivity"); toggleDataConnectivity.setAccessible(true); toggleDataConnectivity.invoke(objITelephony); } // Requires: androID.permission.CHANGE_NETWORK_STATE else if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LolliPOP){ final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); // Gingerbread to KitKat inclusive final FIEld serviceFIEld = connMgr.getClass().getDeclaredFIEld("mService"); serviceFIEld.setAccessible(true); final Object connService = serviceFIEld.get(connMgr); try{ final Method setMobileDataEnabled = connService.getClass() .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); setMobileDataEnabled.setAccessible(true); setMobileDataEnabled.invoke(connService, Boolean.valueOf(enabled)); } catch(NoSuchMethodException e){ // Support for CyanogenMod 11+ final Method setMobileDataEnabled = connService.getClass() .getDeclaredMethod("setMobileDataEnabled", String.class, Boolean.TYPE); setMobileDataEnabled.setAccessible(true); setMobileDataEnabled.invoke(connService, context.getPackagename(), Boolean.valueOf(enabled)); } } // Requires: androID.permission.MODIFY_PHONE_STATE (System only, here for completions sake) else{ // Lollipop and into the Future! final TelephonyManager telMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); final Method setDataEnabled = telMgr.getClass().getDeclaredMethod("setDataEnabled", Boolean.TYPE); setDataEnabled.setAccessible(true); setDataEnabled.invoke(telMgr, Boolean.valueOf(enabled)); } return true; } catch(NoSuchFIEldException e){ Log.e(TAG, "setMobileConnectionEnabled", e); } catch(illegalaccessexception e){ Log.e(TAG, "setMobileConnectionEnabled", e); } catch(IllegalArgumentException e){ Log.e(TAG, "setMobileConnectionEnabled", e); } catch(NoSuchMethodException e){ Log.e(TAG, "setMobileConnectionEnabled", e); } catch(InvocationTargetException e){ Log.e(TAG, "setMobileConnectionEnabled", e); } return false;}需要权限.
<uses-permission androID:name="androID.permission.CHANGE_NETWORK_STATE"/> 总结 以上是内存溢出为你收集整理的重置Android移动网络信号?全部内容,希望文章能够帮你解决重置Android移动网络信号?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)